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

openai[patch]: support optional fields in dict structured output with method="json_schema" #29075

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ccurme
Copy link
Collaborator

@ccurme ccurme commented Jan 7, 2025

The json_schema method will currently error on the following:

Optional fields in TypedDict:

class Entity(TypedDict):
    """Extracted entity."""

    animal: Annotated[str, ..., "The animal"]
    color: Annotated[Optional[str], None, "The color"]

structured_llm = llm.with_structured_output(Entity, method="json_schema")
structured_llm.invoke("black cat")

Optional fields in JSON schema generated from a Pydantic object:

class Entity(BaseModel):
    """Extracted entity."""
    animal: str = Field(description="The animal")
    color: Optional[str] = Field(default=None, description="The color")

structured_llm = llm.with_structured_output(Entity.model_json_schema(), method="json_schema")
structured_llm.invoke("black cat")

Fields with any non-null default (Pydantic, TypedDict, or JSON schema):

class Entity(BaseModel):
    """Extracted entity."""
    animal: str = Field(description="The animal")
    color: str = Field(default="blue", description="The color")

structured_llm = llm.with_structured_output(Entity, method="json_schema")
structured_llm.invoke("black cat")

Here, we update _convert_to_openai_response_format to update optional fields to be required with possible 'null' type. This is a solution recommended in OpenAI's docs.

Note that we make no changes to schemas specified as Pydantic models.

TODO:

  • support nested schemas for TypedDict, which also currently raise an error.
  • run new standard test across providers

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 7, 2025
Copy link

vercel bot commented Jan 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Jan 7, 2025 8:01pm

@dosubot dosubot bot added langchain Related to the langchain package 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Jan 7, 2025
@ccurme ccurme added 🔌: openai Primarily related to OpenAI integrations and removed langchain Related to the langchain package labels Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: Internal
Development

Successfully merging this pull request may close these issues.

1 participant