openai[patch]: detect old models in with_structured_output (#29392)

Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
Bagatur
2025-01-23 12:47:32 -08:00
committed by GitHub
parent b6ae7ca91d
commit 8d566a8fe7
6 changed files with 592 additions and 330 deletions

View File

@@ -1354,19 +1354,33 @@ class BaseChatOpenAI(BaseChatModel):
)
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: