fix: skip phantom empty AI message from Responses API streaming#658
Open
cristipufu wants to merge 1 commit intomainfrom
Open
fix: skip phantom empty AI message from Responses API streaming#658cristipufu wants to merge 1 commit intomainfrom
cristipufu wants to merge 1 commit intomainfrom
Conversation
When using the OpenAI Responses API (`use_responses_api=True`), the `response.created` SSE event produces an AIMessageChunk with a unique `id` (e.g. `resp_xxx`) but no content. All subsequent chunks (text deltas, tool calls, response.completed) arrive with `id=None`, which LangChain's BaseChatModel reassigns to a callback `run_id`. This ID mismatch causes the mapper to emit a phantom empty `message_start` for the `resp_` chunk that never receives content or an end event, resulting in a duplicate empty "green dot" AI message in the chat UI. The fix adds a guard in `map_ai_message_chunk_to_events()` that skips registering a new message for chunks that carry no payload (no content, content_blocks, tool_call_chunks, or tool_calls) and are not the final chunk. This silently drops the metadata-only `response.created` chunk while preserving all other streaming behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UiPathChatOpenAIwithuse_responses_api=Trueresponse.createdSSE event produces anAIMessageChunkwithid=resp_xxxand empty content, while all subsequent chunks getid=None(reassigned torun-xxxby LangChain). The mapper treats these as two separate messages — one phantom, one real.map_ai_message_chunk_to_events()that silently drops metadata-only chunks (no content, content_blocks, tool_call_chunks, or tool_calls) that aren't the final chunkRoot cause details
In
langchain_openai,_convert_responses_chunk_to_generation_chunk()initializesid = Nonefor every chunk but setsid = chunk.response.idonly forresponse.created. LangChain'sBaseChatModelthen assignsrun_idto all chunks withid=None. This creates two different IDs in the stream:response.createdresp_abc123response.output_text.deltarun-xxx(wasNone)response.completedrun-xxx(wasNone)chunk_position="last"The upstream bug exists in
langchain-openai(related: langchain#34138, closed without merge). This PR adds a defensive guard in the mapper layer.Test plan
test_map_event_skips_empty_metadata_chunk_with_new_id— verifies phantom chunks are droppedtest_map_event_responses_api_no_phantom_message— end-to-end Responses API simulation🤖 Generated with Claude Code