v0.30.0
What's Changed
Warning
Breaking change: StructuredOutputError
has been replaced by more specific exceptions StringNotAllowedError
and ToolSchemaParseError
in PR #288
🤖 ♻️ LLM-Assisted retries has been added. When enabled, this sends incorrectly formatted output back to the LLM along with the error message to have the LLM fix its mistakes. This can be used to enforce more complex validation on output schemas using pydantic validators.
For example, placing an arbitrary constraint on a string field
from typing import Annotated
from magentic import prompt
from pydantic import AfterValidator, BaseModel
def assert_is_ireland(v: str) -> str:
if v != "Ireland":
raise ValueError("Country must be Ireland")
return v
class Country(BaseModel):
name: Annotated[str, AfterValidator(assert_is_ireland)]
capital: str
@prompt(
"Return a country",
max_retries=3,
)
def get_country() -> Country: ...
get_country()
# 05:13:55.607 Calling prompt-function get_country
# 05:13:55.622 LLM-assisted retries enabled. Max 3
# 05:13:55.627 Chat Completion with 'gpt-4o' [LLM]
# 05:13:56.309 streaming response from 'gpt-4o' took 0.11s [LLM]
# 05:13:56.310 Retrying Chat Completion. Attempt 1.
# 05:13:56.322 Chat Completion with 'gpt-4o' [LLM]
# 05:13:57.456 streaming response from 'gpt-4o' took 0.00s [LLM]
#
# Country(name='Ireland', capital='Dublin')
See the new docs page on Retrying for more info.
PRs
- Bump aiohttp from 3.9.5 to 3.10.2 by @dependabot in #297
- Add LLM-assisted retries by @jackmpcollins in #288
- Set logfire OTEL scope to magentic by @jackmpcollins in #298
Full Changelog: v0.29.0...v0.30.0