The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(openai): handle `AIMessage`s without `response_id` in `_get_last_messages` (#32824)
Sadiq Khan228fbac3a6bf
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.pyindex 6f7c386776..38a4568d90 100644--- a/libs/partners/openai/langchain_openai/chat_models/base.py+++ b/libs/partners/openai/langchain_openai/chat_models/base.py@@ -3524,8 +3524,7 @@ def _get_last_messages(response_id = msg.response_metadata.get("id")if response_id:return messages[i + 1 :], response_id- else:- return messages, None+ # Continue searching for an AIMessage with a valid response_idreturn messages, Nonediff --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 2c33fe062b..cf24d857de 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@@ -2512,6 +2512,42 @@ def test_get_last_messages() -> None:assert response_id == "resp_123"+def test_get_last_messages_with_mixed_response_metadata() -> None:+ """Test that _get_last_messages correctly skips AIMessages without response_id."""+ # Test case where the most recent AIMessage has no response_id,+ # but an earlier AIMessage does have one+ messages = [+ HumanMessage("Hello"),+ AIMessage("Hi there!", response_metadata={"id": "resp_123"}),+ HumanMessage("How are you?"),+ AIMessage("I'm good"), # No response_metadata+ HumanMessage("What's up?"),+ ]+ last_messages, previous_response_id = _get_last_messages(messages)+ # Should return messages after the AIMessage+ # with response_id (not the most recent one)++ assert last_messages == [+ HumanMessage("How are you?"),+ AIMessage("I'm good"),+ HumanMessage("What's up?"),+ ]+ assert previous_response_id == "resp_123"++ # Test case where no AIMessage has response_id+ messages = [+ HumanMessage("Hello"),+ AIMessage("Hi there!"), # No response_metadata+ HumanMessage("How are you?"),+ AIMessage("I'm good"), # No response_metadata+ HumanMessage("What's up?"),+ ]+ last_messages, previous_response_id = _get_last_messages(messages)+ # Should return all messages when no AIMessage has response_id+ assert last_messages == messages+ assert previous_response_id is None++def test_get_request_payload_use_previous_response_id() -> None:# Default - don't use previous_response IDllm = ChatOpenAI(