fix: properly wrap Vertex AI beta streaming responses #1021
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
Fixes tool input parameters being lost when using Claude models via Vertex AI with streaming enabled. The Vertex AI beta messages implementation was returning plain
Stream
objects instead ofBetaMessageStreamManager
, bypassing the event accumulation logic entirely.Closes #1020
Problem
When using the beta messages API with Vertex AI and streaming enabled (
stream=True
), tool calls fail with validation errors because tool inputs remain empty{}
.The root cause:
FirstPartyMessagesAPI.create
Stream
instead ofBetaMessageStreamManager
BetaMessageStream
was never reachedinput_json_delta
events, but they weren't being accumulatedSolution
This PR overrides the
create()
andstream()
methods in/src/anthropic/lib/vertex/_beta_messages.py
to ensure:create()
method wrapsStream
inBetaMessageStream
whenstream=True
stream()
method returnsBetaMessageStreamManager
for proper event accumulationinput_json_delta
events as intendedChanges Made
src/anthropic/lib/vertex/_beta_messages.py
:Messages.create()
to wrapStream
inBetaMessageStream
when streamingMessages.stream()
to returnBetaMessageStreamManager
AsyncMessages.create()
to wrapAsyncStream
inBetaAsyncMessageStream
when streamingAsyncMessages.stream()
to returnBetaAsyncMessageStreamManager
BetaMessageStream
andBetaAsyncMessageStream
Testing
Manual Testing
Automated Tests
Test Output
Impact
Verification Checklist
pytest tests/lib/test_vertex.py
)ruff check
)Related Context
Example Usage After Fix