fix(agent): shape partial-stream stub to match active api_mode - #46003
Closed
liuhao1024 wants to merge 1 commit into
Closed
liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
When a malformed Anthropic stream (non-contiguous content_block indices) crashes stream.get_final_message() with IndexError, the partial-stream recovery stub now returns an Anthropic-shaped response (content list) for anthropic_messages mode instead of always returning an OpenAI-shaped one (choices list). Previously AnthropicTransport.validate_response() rejected the stub because it checks response.content, which the OpenAI stub lacks — causing 10 useless retries in the conversation loop. Also extend _is_provider_stream_parse_error() to classify IndexError as a stream parse error for anthropic_messages mode, enabling the transient-retry path for upstream protocol violations. Fixes NousResearch#45908
Contributor
Contributor
Author
|
Closing as duplicate of #45919 by @LeonSGP43, which implements the same fix for #45908 (Anthropic-shaped partial-stream recovery stub). That PR extracts a cleaner helper function with complete field coverage. It was opened earlier today and has passing CI. |
19 tasks
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.
What does this PR do?
Fixes the partial-stream recovery stub for
anthropic_messagesmode. When a malformed Anthropic stream (non-contiguouscontent_blockindices) crashesstream.get_final_message()withIndexError, the recovery stub now returns an Anthropic-shaped response (contentlist) instead of an OpenAI-shaped one (choiceslist). Previously the stub always used OpenAI shape, whichAnthropicTransport.validate_response()rejected, causing 10 useless retries in the conversation loop.Related Issue
Fixes #45908
Type of Change
Changes Made
agent/chat_completion_helpers.py: In the partial-stream stub creation path, checkagent.api_mode. When"anthropic_messages", return an Anthropic-shaped stub withcontent=[SimpleNamespace(type="text", text=...)]andstop_reason="max_tokens"instead of the OpenAI-shapedchoices=[...]stub. This ensuresAnthropicTransport.validate_response()accepts the stub on the first try.run_agent.py: Extend_is_provider_stream_parse_error()to also classifyIndexErroras a stream parse error foranthropic_messagesmode. Non-contiguouscontent_blockindices from upstream proxies cause the SDK accumulator to raiseIndexError— this is an upstream protocol violation that should follow the same retry path as malformed event-stream frames.tests/agent/test_anthropic_stream_stub_shape.py: 8 regression tests covering: Anthropic stub hascontentlist, passesvalidate_response, normalizes correctly,chat_completionsstub unchanged,IndexErrorclassified as parse error, mode-gated behavior, existingValueErrorclassification preserved.How to Test
pytest tests/agent/test_anthropic_stream_stub_shape.py -xvs— all 8 tests passpytest tests/run_agent/test_partial_stream_finish_reason.py -xvs— all 9 existing tests pass (no regression)Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
agent/chat_completion_helpers.py::interruptible_streaming_api_call(stub creation path),run_agent.py::_is_provider_stream_parse_error(error classification)PARTIAL_STREAM_STUB_ID(stub identification),AnthropicTransport.validate_response()(validator),_STOP_REASON_MAP(stop_reason → finish_reason mapping)