Skip to content

Commit 8d566a8

Browse files
baskaryanccurme
andauthored
openai[patch]: detect old models in with_structured_output (#29392)
Co-authored-by: ccurme <chester.curme@gmail.com>
1 parent b6ae7ca commit 8d566a8

File tree

6 files changed

+592
-330
lines changed

6 files changed

+592
-330
lines changed

libs/partners/openai/langchain_openai/chat_models/base.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,19 +1354,33 @@ def with_structured_output(
13541354
)
13551355
is_pydantic_schema = _is_pydantic_class(schema)
13561356

1357-
# Check for Pydantic BaseModel V1
1358-
if (
1359-
method == "json_schema"
1360-
and is_pydantic_schema
1361-
and issubclass(schema, BaseModelV1) # type: ignore[arg-type]
1362-
):
1363-
warnings.warn(
1364-
"Received a Pydantic BaseModel V1 schema. This is not supported by "
1365-
'method="json_schema". Please use method="function_calling" '
1366-
"or specify schema via JSON Schema or Pydantic V2 BaseModel. "
1367-
'Overriding to method="function_calling".'
1368-
)
1369-
method = "function_calling"
1357+
if method == "json_schema":
1358+
# Check for Pydantic BaseModel V1
1359+
if (
1360+
is_pydantic_schema and issubclass(schema, BaseModelV1) # type: ignore[arg-type]
1361+
):
1362+
warnings.warn(
1363+
"Received a Pydantic BaseModel V1 schema. This is not supported by "
1364+
'method="json_schema". Please use method="function_calling" '
1365+
"or specify schema via JSON Schema or Pydantic V2 BaseModel. "
1366+
'Overriding to method="function_calling".'
1367+
)
1368+
method = "function_calling"
1369+
# Check for incompatible model
1370+
if self.model_name and (
1371+
self.model_name.startswith("gpt-3")
1372+
or self.model_name.startswith("gpt-4-")
1373+
or self.model_name == "gpt-4"
1374+
):
1375+
warnings.warn(
1376+
f"Cannot use method='json_schema' with model {self.model_name} "
1377+
f"since it doesn't support OpenAI's Structured Output API. You can "
1378+
f"see supported models here: "
1379+
f"https://platform.openai.com/docs/guides/structured-outputs#supported-models. " # noqa: E501
1380+
"To fix this warning, set `method='function_calling'. "
1381+
"Overriding to method='function_calling'."
1382+
)
1383+
method = "function_calling"
13701384

13711385
if method == "function_calling":
13721386
if schema is None:

0 commit comments

Comments
 (0)