Skip to content

The agent engineering platform.

fix(openai): filter langchain-generated content block IDs (#39209)

ccurmedd6081977099
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py
index 16516376c6..576b24f7eb 100644
--- a/libs/partners/openai/langchain_openai/chat_models/base.py
+++ b/libs/partners/openai/langchain_openai/chat_models/base.py
@@ -118,7 +118,7 @@ from langchain_core.utils.pydantic import (
TypeBaseModel,
is_basemodel_subclass,
)
-from langchain_core.utils.utils import _build_model_kwargs, from_env
+from langchain_core.utils.utils import LC_AUTO_PREFIX, _build_model_kwargs, from_env
from pydantic import (
BaseModel,
ConfigDict,
@@ -4670,7 +4670,14 @@ def _construct_responses_api_input(
"content": [new_block],
"role": "assistant",
}
- if store is not False:
+ if (
+ store is not False
+ and msg_id
+ and not msg_id.startswith(LC_AUTO_PREFIX)
+ ):
+ # LangChain-auto-generated (`lc_`) IDs are not
+ # provider-issued; the Responses API rejects
+ # them (expects `msg_...`), so don't forward.
new_item["id"] = msg_id
if phase is not None:
new_item["phase"] = phase
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 cab0ed0734..e4c953f6d2 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
@@ -2985,6 +2985,34 @@ def test__construct_responses_api_input_skips_blocks_without_text() -> None:
}
+def test_responses_api_payload_drops_lc_auto_ids() -> None:
+ """LangChain-auto-generated (`lc_`) IDs must not be forwarded to OpenAI.
+
+ The Responses API validates provider-issued message IDs (expects `msg_...`),
+ so synthetic AI messages built from `create_text_block` (which assigns an
+ `lc_`-prefixed ID) previously triggered a 400. See issue #39113.
+ """
+ text_block = types.create_text_block("The capital of France is Paris.")
+ assert text_block["id"].startswith("lc_")
+
+ llm = ChatOpenAI(model=OPENAI_TEST_MODEL, use_responses_api=True)
+ payload = llm._get_request_payload([AIMessage(content_blocks=[text_block])])
+
+ input_ = payload["input"]
+ assert len(input_) == 1
+ assert input_[0]["type"] == "message"
+ assert input_[0]["role"] == "assistant"
+ # The `lc_` ID is dropped rather than forwarded to OpenAI.
+ assert "id" not in input_[0]
+ assert input_[0]["content"] == [
+ {
+ "type": "output_text",
+ "text": "The capital of France is Paris.",
+ "annotations": [],
+ }
+ ]
+
+
def test__construct_responses_api_input_human_message_with_image_url_conversion() -> (
None
):