The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(openai): suppress Pydantic serializer warning on structured output parsed field (#37727)
Naomifa0114511b26
diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.pyindex 132c0e207b..dec8e627d5 100644--- a/libs/partners/openai/langchain_openai/chat_models/azure.py+++ b/libs/partners/openai/langchain_openai/chat_models/azure.py@@ -771,7 +771,8 @@ class AzureChatOpenAI(BaseChatOpenAI):chat_result = super()._create_chat_result(response, generation_info)if not isinstance(response, dict):- response = response.model_dump()+ # warnings=False due to https://github.com/openai/openai-python/issues/2872+ response = response.model_dump(warnings=False)for res in response["choices"]:if res.get("finish_reason", None) == "content_filter":msg = (diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.pyindex 1181a80a26..518c471be0 100644--- a/libs/partners/openai/langchain_openai/chat_models/base.py+++ b/libs/partners/openai/langchain_openai/chat_models/base.py@@ -1761,7 +1761,8 @@ class BaseChatOpenAI(BaseChatModel):# `parsed` may hold arbitrary Pydantic models from structured output.# Exclude it from this dump and copy it from the typed response below.else response.model_dump(- exclude={"choices": {"__all__": {"message": {"parsed"}}}}+ exclude={"choices": {"__all__": {"message": {"parsed"}}}},+ warnings=False,))# Sometimes the AI Model calling will get error, we should raise it (this is@@ -4669,7 +4670,10 @@ def _construct_lc_result_from_responses_api(response_metadata = {k: v- for k, v in response.model_dump(exclude_none=True, mode="json").items()+ # warnings=False due to https://github.com/openai/openai-python/issues/2872+ for k, v in response.model_dump(+ exclude_none=True, mode="json", warnings=False+ ).items()if kin ("created_at",diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py b/libs/partners/openai/tests/unit_tests/chat_models/test_azure.pyindex ddd53725f3..19c9a194be 100644--- a/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py+++ b/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py@@ -1,11 +1,13 @@"""Test Azure OpenAI Chat API wrapper."""import os+import warningsfrom unittest import mock+import openaiimport pytestfrom langchain_core.messages import HumanMessage-from pydantic import SecretStr+from pydantic import BaseModel, SecretStrfrom typing_extensions import TypedDictfrom langchain_openai import AzureChatOpenAI@@ -282,3 +284,59 @@ def test_metadata_versions() -> None:versions = llm.metadata["lc_versions"]assert "langchain-core" in versionsassert "langchain-openai" in versions+++def test_create_chat_result_avoids_parsed_model_dump_warning() -> None:+ """Azure structured output must not emit a serializer warning.++ `AzureChatOpenAI._create_chat_result` re-dumps the response to read content-filter+ metadata; with structured output the SDK's `parsed` field holds a model that+ doesn't match its declared type (openai/openai-python#2872).+ """+ from openai.lib._parsing._completions import parse_chat_completion+ from openai.types.chat import ChatCompletion+ from openai.types.chat.chat_completion import Choice+ from openai.types.chat.chat_completion_message import ChatCompletionMessage++ class ModelOutput(BaseModel):+ output: str++ raw_response = ChatCompletion(+ id="chatcmpl-1",+ object="chat.completion",+ created=0,+ model="gpt-4o-mini",+ choices=[+ Choice(+ index=0,+ finish_reason="stop",+ message=ChatCompletionMessage(+ role="assistant", content='{"output": "Paris"}'+ ),+ )+ ],+ )+ parsed_response = parse_chat_completion(+ response_format=ModelOutput,+ input_tools=openai.omit,+ chat_completion=raw_response,+ )++ llm = AzureChatOpenAI( # type: ignore[call-arg]+ azure_deployment="35-turbo-dev",+ openai_api_version="2023-05-15",+ azure_endpoint="my-base-url",+ api_key=SecretStr("test"),+ )+ with warnings.catch_warnings(record=True) as caught_warnings:+ warnings.simplefilter("always")+ result = llm._create_chat_result(parsed_response)++ warning_messages = [str(warning.message) for warning in caught_warnings]+ assert not any(+ "PydanticSerializationUnexpectedValue" in message+ for message in warning_messages+ )+ assert result.generations[0].message.additional_kwargs["parsed"] == ModelOutput(+ output="Paris"+ )diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_base.py b/libs/partners/openai/tests/unit_tests/chat_models/test_base.pyindex 1be3d52326..fc690a1e34 100644--- a/libs/partners/openai/tests/unit_tests/chat_models/test_base.py+++ b/libs/partners/openai/tests/unit_tests/chat_models/test_base.py@@ -36,6 +36,7 @@ from langchain_core.runnables import RunnableLambdafrom langchain_core.runnables.base import RunnableBinding, RunnableSequencefrom langchain_core.tracers.base import BaseTracerfrom langchain_core.tracers.schemas import Run+from langchain_core.utils.pydantic import PYDANTIC_VERSIONfrom openai.types.responses import (ResponseApplyPatchToolCall,ResponseApplyPatchToolCallOutput,@@ -1702,36 +1703,39 @@ def test_structured_outputs_parser() -> None:def test_create_chat_result_avoids_parsed_model_dump_warning() -> None:+ """Chat Completions structured output must not emit a serializer warning.++ Built via the SDK's own `parse_chat_completion` (the path+ `client.beta.chat.completions.parse(...)` uses) so the test exercises the real+ `ParsedChatCompletion` typing (openai/openai-python#2872).+ """+ from openai.lib._parsing._completions import parse_chat_completion+ from openai.types.chat import ChatCompletion+ from openai.types.chat.chat_completion import Choice+ from openai.types.chat.chat_completion_message import ChatCompletionMessage+class ModelOutput(BaseModel):output: str- class MockParsedMessage(openai.BaseModel):- role: Literal["assistant"] = "assistant"- content: str = '{"output": "Paris"}'- parsed: None = None- refusal: str | None = None-- class MockChoice(openai.BaseModel):- index: int = 0- finish_reason: Literal["stop"] = "stop"- message: MockParsedMessage-- class MockChatCompletion(openai.BaseModel):- id: str = "chatcmpl-1"- object: str = "chat.completion"- created: int = 0- model: str = OPENAI_TEST_MODEL- choices: list[MockChoice]- usage: dict[str, int] | None = None-- parsed_response = ModelOutput(output="Paris")- response = MockChatCompletion.model_construct(+ raw_response = ChatCompletion(+ id="chatcmpl-1",+ object="chat.completion",+ created=0,+ model=OPENAI_TEST_MODEL,choices=[- MockChoice.model_construct(- message=MockParsedMessage.model_construct(parsed=parsed_response)+ Choice(+ index=0,+ finish_reason="stop",+ message=ChatCompletionMessage(+ role="assistant", content='{"output": "Paris"}'+ ),)],- usage={"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},+ )+ response = parse_chat_completion(+ response_format=ModelOutput,+ input_tools=openai.omit,+ chat_completion=raw_response,)llm = ChatOpenAI(model=OPENAI_TEST_MODEL)@@ -1740,8 +1744,82 @@ def test_create_chat_result_avoids_parsed_model_dump_warning() -> None:result = llm._create_chat_result(response)warning_messages = [str(warning.message) for warning in caught_warnings]- assert not any("field_name='parsed'" in message for message in warning_messages)- assert result.generations[0].message.additional_kwargs["parsed"] == parsed_response+ assert not any(+ "PydanticSerializationUnexpectedValue" in message+ for message in warning_messages+ )+ assert result.generations[0].message.additional_kwargs["parsed"] == ModelOutput(+ output="Paris"+ )+++ (PYDANTIC_VERSION.major, PYDANTIC_VERSION.minor) < (2, 8),+ reason=(+ "Serializing the generic `ParsedResponse` raises a `MockValSer` TypeError on "+ "pydantic<2.8, independent of the warning under test."+ ),+)+def test__construct_lc_result_from_responses_api_avoids_parsed_dump_warning() -> None:+ """Responses API structured output must not emit serializer warnings.++ With `use_responses_api=True` and structured output, the SDK returns a+ `ParsedResponse` whose output items don't match their declared field/union types.+ Dumping it for response metadata previously emitted a spurious+ `PydanticSerializationUnexpectedValue` warning (openai/openai-python#2872).+ """+ from openai.lib._parsing._responses import parse_response++ class DocumentScoreResult(BaseModel):+ reasoning: str+ relevance_score: int++ # Build the object the way `client.responses.parse(text_format=...)` does, so the+ # test exercises the real `ParsedResponse` typing rather than a hand-rolled mock.+ raw_response = Response(+ id="resp_123",+ created_at=1234567890,+ model=OPENAI_TEST_MODEL,+ object="response",+ parallel_tool_calls=True,+ tools=[],+ tool_choice="auto",+ output=[+ ResponseOutputMessage(+ type="message",+ id="msg_123",+ status="completed",+ role="assistant",+ content=[+ ResponseOutputText(+ type="output_text",+ text='{"reasoning": "relevant", "relevance_score": 1}',+ annotations=[],+ )+ ],+ )+ ],+ )+ parsed_response = parse_response(+ text_format=DocumentScoreResult,+ input_tools=openai.omit,+ response=raw_response,+ )++ with warnings.catch_warnings(record=True) as caught_warnings:+ warnings.simplefilter("always")+ result = _construct_lc_result_from_responses_api(+ parsed_response, schema=DocumentScoreResult+ )++ warning_messages = [str(warning.message) for warning in caught_warnings]+ assert not any(+ "PydanticSerializationUnexpectedValue" in message+ for message in warning_messages+ )+ assert result.generations[0].message.additional_kwargs[+ "parsed"+ ] == DocumentScoreResult(reasoning="relevant", relevance_score=1)def test_structured_outputs_parser_valid_falsy_response() -> None: