Skip to content

Commit

Permalink
openai[patch]: fix schema formatting util (#27685)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Oct 28, 2024
1 parent 440c162 commit ede953d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
8 changes: 6 additions & 2 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,9 +2143,13 @@ def _convert_to_openai_response_format(
if isinstance(schema, type) and is_basemodel_subclass(schema):
return schema

if "json_schema" in schema and schema.get("type") == "json_schema":
if (
isinstance(schema, dict)
and "json_schema" in schema
and schema.get("type") == "json_schema"
):
response_format = schema
elif "name" in schema and "schema" in schema:
elif isinstance(schema, dict) and "name" in schema and "schema" in schema:
response_format = {"type": "json_schema", "json_schema": schema}
else:
strict = strict if strict is not None else True
Expand Down
42 changes: 41 additions & 1 deletion libs/partners/openai/tests/unit_tests/chat_models/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
ToolMessage,
)
from langchain_core.messages.ai import UsageMetadata
from pydantic import BaseModel
from pydantic import BaseModel, Field
from typing_extensions import TypedDict

from langchain_openai import ChatOpenAI
from langchain_openai.chat_models.base import (
Expand Down Expand Up @@ -805,3 +806,42 @@ def test__convert_to_openai_response_format() -> None:

with pytest.raises(ValueError):
_convert_to_openai_response_format(response_format, strict=False)


@pytest.mark.parametrize("method", ["function_calling", "json_schema"])
@pytest.mark.parametrize("strict", [True, None])
def test_structured_output_strict(
method: Literal["function_calling", "json_schema"], strict: Optional[bool]
) -> None:
"""Test to verify structured output with strict=True."""

llm = ChatOpenAI(model="gpt-4o-2024-08-06")

class Joke(BaseModel):
"""Joke to tell user."""

setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")

llm.with_structured_output(Joke, method=method, strict=strict)
# Schema
llm.with_structured_output(Joke.model_json_schema(), method=method, strict=strict)


def test_nested_structured_output_strict() -> None:
"""Test to verify structured output with strict=True for nested object."""

llm = ChatOpenAI(model="gpt-4o-2024-08-06")

class SelfEvaluation(TypedDict):
score: int
text: str

class JokeWithEvaluation(TypedDict):
"""Joke to tell user."""

setup: str
punchline: str
self_evaluation: SelfEvaluation

llm.with_structured_output(JokeWithEvaluation, method="json_schema")

0 comments on commit ede953d

Please sign in to comment.