fix(streaming): add direct type-map fallback for raw dict events (fixes #941)#1448
Open
Zelys-DFKH wants to merge 1 commit intoanthropics:mainfrom
Open
fix(streaming): add direct type-map fallback for raw dict events (fixes #941)#1448Zelys-DFKH wants to merge 1 commit intoanthropics:mainfrom
Zelys-DFKH wants to merge 1 commit intoanthropics:mainfrom
Conversation
Fixes anthropics#941 — `content_block_delta` SSE events fail to deserialize with "Unexpected event runtime type, after deserialising twice" in some environments where Pydantic's discriminator resolution silently returns the raw dict instead of raising an error or constructing the proper model. Root Cause: The union discriminator deserialization in older Pydantic versions can silently fall back to returning the raw dict when the discriminator lookup fails, rather than raising an explicit error. Solution: Add a fallback type-map lookup when `construct_type_unchecked` returns a dict instead of a BaseModel. Use the 'type' field from the dict to look up the correct event class and construct it directly via `model_construct`. Changes: - Added event type imports to _messages.py and _beta_messages.py - Added _RAW_EVENT_TYPE_MAP and _BETA_RAW_EVENT_TYPE_MAP dictionaries - Updated error handling in accumulate_event to use the fallback when discriminator deserialization returns a dict This fix ensures that well-formed events (including content_block_delta) are always promoted to the correct BaseModel, even in environments where the union discriminator mechanism fails silently. Co-Authored-By: Zelys-DFKH <admin@dfkhelper.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.
Problem
The
accumulate_eventfunction in the streaming message parser fails with:This occurs when SSE events like
content_block_deltaare received as raw dicts instead of properly deserialized Pydantic models.Root Cause
Pydantic's union discriminator can fail silently in certain versions or configurations. Instead of raising an error,
construct_type_uncheckedfalls back to returning the raw dict—making the failure invisible until the type check fails later.Solution
Add a fallback type-map lookup when the dict is returned. This approach:
_RAW_EVENT_TYPE_MAPand_BETA_RAW_EVENT_TYPE_MAPdictionaries that map event type strings to their corresponding Pydantic model classestypefield to look up the correct event classmodel_construct(**raw_dict)The fallback catches the silent failure early. Well-formed events now construct correctly, and malformed events still raise the original error—so we get both robustness and clear error messages.
Changes
RawMessageStreamEventunion members to both_messages.pyand_beta_messages.py_RAW_EVENT_TYPE_MAPand_BETA_RAW_EVENT_TYPE_MAPdictionariesaccumulate_eventto implement the fallback before raising an errorTesting
This fix has been tested to handle:
content_block_deltaevents arriving as raw dicts