Skip to content

Commit

Permalink
openai[patch]: detect old models in with_structured_output (#29392)
Browse files Browse the repository at this point in the history
Co-authored-by: ccurme <[email protected]>
  • Loading branch information
baskaryan and ccurme authored Jan 23, 2025
1 parent b6ae7ca commit 8d566a8
Show file tree
Hide file tree
Showing 6 changed files with 592 additions and 330 deletions.
40 changes: 27 additions & 13 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,19 +1354,33 @@ def with_structured_output(
)
is_pydantic_schema = _is_pydantic_class(schema)

# Check for Pydantic BaseModel V1
if (
method == "json_schema"
and is_pydantic_schema
and issubclass(schema, BaseModelV1) # type: ignore[arg-type]
):
warnings.warn(
"Received a Pydantic BaseModel V1 schema. This is not supported by "
'method="json_schema". Please use method="function_calling" '
"or specify schema via JSON Schema or Pydantic V2 BaseModel. "
'Overriding to method="function_calling".'
)
method = "function_calling"
if method == "json_schema":
# Check for Pydantic BaseModel V1
if (
is_pydantic_schema and issubclass(schema, BaseModelV1) # type: ignore[arg-type]
):
warnings.warn(
"Received a Pydantic BaseModel V1 schema. This is not supported by "
'method="json_schema". Please use method="function_calling" '
"or specify schema via JSON Schema or Pydantic V2 BaseModel. "
'Overriding to method="function_calling".'
)
method = "function_calling"
# Check for incompatible model
if self.model_name and (
self.model_name.startswith("gpt-3")
or self.model_name.startswith("gpt-4-")
or self.model_name == "gpt-4"
):
warnings.warn(
f"Cannot use method='json_schema' with model {self.model_name} "
f"since it doesn't support OpenAI's Structured Output API. You can "
f"see supported models here: "
f"https://platform.openai.com/docs/guides/structured-outputs#supported-models. " # noqa: E501
"To fix this warning, set `method='function_calling'. "
"Overriding to method='function_calling'."
)
method = "function_calling"

if method == "function_calling":
if schema is None:
Expand Down
Loading

0 comments on commit 8d566a8

Please sign in to comment.