What's Changed
Add support for OpenAI "strict" setting for structured outputs. This guarantees that the generated JSON schema matches that supplied by the user. In magentic, this is set via an extension of pydantic's `ConfigDict`, and works for pydantic models as well as functions. See the docs for more info https://magentic.dev/structured-outputs/#configdict
For a BaseModel
python
from magentic import prompt, ConfigDict
from pydantic import BaseModel
class Superhero(BaseModel):
model_config = ConfigDict(openai_strict=True)
name: str
age: int
power: str
enemies: list[str]
prompt("Create a Superhero named {name}.")
def create_superhero(name: str) -> Superhero: ...
create_superhero("Garden Man")
For a function
python
from typing import Annotated, Literal
from magentic import ConfigDict, with_config
from pydantic import Field
with_config(ConfigDict(openai_strict=True))
def activate_oven(
temperature: Annotated[int, Field(description="Temp in Fahrenheit", lt=500)],
mode: Literal["broil", "bake", "roast"],
) -> str:
"""Turn the oven on with the provided settings."""
return f"Preheating to {temperature} F with mode {mode}"
prompt(
"Do some cooking",
functions=[
activate_oven,
...
PRs
* Add support for OpenAI structured outputs by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/305
**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.31.0...v0.32.0