Skip to content

v0.32.0

Latest
Compare
Choose a tag to compare
@jackmpcollins jackmpcollins released this 18 Aug 09:16

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

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

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

Full Changelog: v0.31.0...v0.32.0