Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OpenAI structured outputs #305

Merged
merged 12 commits into from
Aug 18, 2024

Conversation

jackmpcollins
Copy link
Owner

@jackmpcollins jackmpcollins commented Aug 18, 2024

  • Extend pydantic's ConfigDict to add openai_strict: bool
  • Enable pydantic config to be set on functions
  • For all types, pass along the pydantic config (if present) to the pydantic model used for serialization in FunctionSchemas
  • Try read pydantic config when converting type to JSON schema, and use openai strict converter if openai_strict is True
  • Add to docs

Example

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")

See docs for more: http://localhost:8000/structured-outputs/#configdict

Related to #295

@jackmpcollins jackmpcollins self-assigned this Aug 18, 2024
@jackmpcollins jackmpcollins merged commit 225c1b3 into main Aug 18, 2024
1 check passed
@jackmpcollins jackmpcollins deleted the support-openai-strict-output branch August 18, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant