ChatOpenAI (Responses API): streamed chunks switch from resp_… to lc_run--… ids, the stream id doesn't match the final message id
Submission checklist
- This is a bug, not a usage question.
- I added a clear and descriptive title that summarizes this issue.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
- This is not related to the langchain-community package.
- I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
Package (Required)
- langchain
- langchain-openai
- langchain-anthropic
- langchain-classic
- langchain-core
- langchain-model-profiles
- langchain-tests
- langchain-text-splitters
- langchain-chroma
- langchain-deepseek
- langchain-exa
- langchain-fireworks
- langchain-groq
- langchain-huggingface
- langchain-mistralai
- langchain-nomic
- langchain-ollama
- langchain-openrouter
- langchain-perplexity
- langchain-qdrant
- langchain-typesafe
- langchain-xai
- Other / not sure / general
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4.1-mini", use_responses_api=True, output_version="v1")
ids = []
full = None
for chunk in llm.stream("Say hello"):
ids.append(chunk.id)
print(repr(chunk.id), repr(chunk.content))
full = chunk if full is None else full + chunk
print("distinct streamed ids:", sorted(set(ids)))
print("merged id:", full.id)
assert len(set(ids)) == 1, "one response streamed under multiple ids"
assert ids[-1] == full.id, "streamed id does not match final message id"Error Message and Stack Trace (if applicable)
'resp_ptk4…' []
'lc_run--01a0e7e2…' [{'type': 'text', 'text': '', ...}]
'lc_run--01a0e7e2…' [{'type': 'text', 'text': 'Hello', ...}]
'lc_run--01a0e7e2…' [{'type': 'text', 'text': 'Hello.', ...}]
...
distinct streamed ids: ['lc_run--01a0e7e2…', 'resp_ptk4…']
merged id: resp_ptk4…
AssertionError: one response streamed under multiple idsDescription
- I'm streaming
ChatOpenAI(use_responses_api=True)inside a LangGraph agent withstream_mode="messages", and the frontend matches streamed chunks to the final state by message id. - I expect every chunk of one response to carry the same id, and that id to match the final
AIMessage. - Instead, only the
response.createdchunk has theresp_…id. Every later chunk hasid=None, soBaseChatModel.stream/astreamfills inlc_run--<run_id>. When the chunks are merged,add_ai_message_chunksprefers the provider id, so the final message isresp_…:
| Source | id |
|---|---|
First chunk (response.created, no content) |
resp_… |
| All content chunks | lc_run--… |
Final merged AIMessage / graph state |
resp_… |
The streamed text and the final message end up with different ids. With LangGraph stream_mode="messages", a client that adds streamed messages by id and then reconciles with the graph state (e.g. assistant-ui's useLangGraphRuntime) shows the response twice. Code that later replaces the message by id, such as an after_model middleware that rewrites content and keeps the id, also misses the streamed copy.
Other integrations are consistent. ChatAnthropic (langchain-anthropic 1.5.4) and ChatBedrockConverse (langchain-aws 1.7.8) never set a provider id on stream chunks. Every chunk and the merged message share one lc_run--… id:
| Integration | Streamed chunk ids | Final merged id | Consistent? |
|---|---|---|---|
ChatAnthropic |
lc_run--… on every chunk |
lc_run--… |
Yes |
ChatBedrockConverse |
lc_run--… on every chunk |
lc_run--… |
Yes |
ChatOpenAI(use_responses_api=True) |
resp_… on the first chunk, then lc_run--… |
resp_… |
No |
Cause. In _convert_responses_chunk_to_generation_chunk, the id is only set on response.created, and each chunk is converted on its own:
elif chunk.type == "response.created":
response = _coerce_chunk_response(chunk.response)
id = response.id
response_metadata["id"] = response.id # Backwards compatibilitySuggested fix. Keep the response.created id in _stream / _astream and apply it to later chunks whose message.id is None. That keeps the provider id and makes the stream consistent. We're using this workaround in a subclass:
def _stamp_response_id(chunk, state):
message = getattr(chunk, "message", None)
if message is None:
return chunk
if message.id is None:
message.id = state.get("response_id")
elif "response_id" not in state:
state["response_id"] = message.id
return chunk
def _stream(self, *args, **kwargs):
state = {}
for chunk in super()._stream(*args, **kwargs):
yield _stamp_response_id(chunk, state)
# same for _astreamI'm happy to open a PR for this
System Info
System Information
OS: Windows OS Version: 10.0.26200 Python Version: 3.14.0 (main, Oct 28 2025, 12:05:23) [MSC v.1944 64 bit (AMD64)]
Package Information
langchain_core: 1.6.5 langchain: 1.3.14 langchain_community: 0.4.2 langsmith: 0.10.17 deepagents: 0.7.5 langchain_agentcore_codeinterpreter: 0.0.5 langchain_anthropic: 1.5.4 langchain_aws: 1.7.8 langchain_classic: 1.0.8 langchain_google_genai: 4.3.3 langchain_mcp_adapters: 0.3.2 langchain_openai: 1.6.6 langchain_protocol: 0.0.18 langchain_text_splitters: 1.1.2 langgraph_sdk: 0.4.2
Social handles (optional)
No response
Source: langchain-ai/langchain