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] get output_type when using with_structured_output #26307

Merged
merged 10 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,10 @@ class AnswerWithJustification(BaseModel):
response_format = _convert_to_openai_response_format(schema, strict=strict)
llm = self.bind(response_format=response_format)
output_parser = (
cast(Runnable, _oai_structured_outputs_parser)
cast(
Runnable,
_oai_structured_outputs_parser.with_types(output_type=schema),
)
if is_pydantic_schema
else JsonOutputParser()
)
Expand Down
17 changes: 17 additions & 0 deletions libs/partners/openai/tests/unit_tests/chat_models/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,20 @@ def test_get_num_tokens_from_messages() -> None:
expected = 176
actual = llm.get_num_tokens_from_messages(messages)
assert expected == actual


def test_schema_from_with_structured_output() -> None:
"""Test schema from with_structured_output."""

class Foo(BaseModel):
bar: Any

model_chat = ChatOpenAI(api_key="foo")

model = model_chat.with_structured_output(Foo, method="json_schema", strict=True)

assert model.get_output_schema().schema() == {
"properties": {"bar": {"title": "Bar"}},
"title": "Foo",
"type": "object",
}
Loading