Skip to content

The agent engineering platform.

fix(openai): raise clear error on unexpected response type in `_create_chat_result` (#39731)

ccurme2019bf5ebe50
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py
index 4c3c58aae6..692b61333f 100644
--- a/libs/partners/openai/langchain_openai/chat_models/base.py
+++ b/libs/partners/openai/langchain_openai/chat_models/base.py
@@ -1881,6 +1881,19 @@ class BaseChatOpenAI(BaseChatModel):
) -> ChatResult:
generations = []
+ if not isinstance(response, dict | openai.BaseModel):
+ # `parse()` yields a `str` when the endpoint returns a non-JSON body,
+ # e.g. an HTML error page served after a redirect.
+ preview = repr(response)
+ if len(preview) > 200:
+ preview = f"{preview[:200]}..."
+ msg = (
+ "Unexpected response type from OpenAI-compatible endpoint. "
+ "Expected a dict or openai.BaseModel, got "
+ f"{type(response).__name__}: {preview}"
+ )
+ raise ValueError(msg)
+
response_dict = (
response
if isinstance(response, dict)
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.py
index 4e3fc62d1b..23db43147c 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
@@ -2100,6 +2100,30 @@ def test_create_chat_result_avoids_parsed_model_dump_warning() -> None:
)
+def test_create_chat_result_raises_on_unexpected_response_type() -> None:
+ """A non-JSON response body must surface a clear error, not an `AttributeError`."""
+ llm = ChatOpenAI(model=OPENAI_TEST_MODEL)
+
+ with pytest.raises(ValueError, match="got str") as exc_info:
+ llm._create_chat_result("<html><body>Moved</body></html>") # type: ignore[arg-type]
+ assert "Moved" in str(exc_info.value)
+
+ with pytest.raises(ValueError, match="got object"):
+ llm._create_chat_result(object()) # type: ignore[arg-type]
+
+
+def test_create_chat_result_truncates_unexpected_response_body() -> None:
+ """A large response body must not be echoed in full in the error message."""
+ llm = ChatOpenAI(model=OPENAI_TEST_MODEL)
+ body = "<html>" + "x" * 5000 + "</html>"
+
+ with pytest.raises(ValueError, match="got str") as exc_info:
+ llm._create_chat_result(body) # type: ignore[arg-type]
+ message = str(exc_info.value)
+ assert len(message) < len(body)
+ assert message.endswith("...")
+
+
@pytest.mark.skipif(
(PYDANTIC_VERSION.major, PYDANTIC_VERSION.minor) < (2, 8),
reason=(