fix: handle type-discriminator format in contentBlockFromData#787
Open
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: handle type-discriminator format in contentBlockFromData#787kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…s-agents#533) When content blocks are stored by ORMs or spread ({...block}) instead of serialized via toJSON(), the data has a flat shape with a type discriminator (e.g. {type: 'toolUseBlock', name, toolUseId, input}) instead of the nested wrapper ({toolUse: {name, toolUseId, input}}). This adds fallback handling in contentBlockFromData for ToolUseBlock, TextBlock, and ToolResultBlock type discriminators, fixing the 'Unknown ContentBlockData type' error when recreating agents from stored session data. Fixes strands-agents#533
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
Fixes #533
When content blocks are stored in a database (e.g., for session persistence) and then loaded back, the data may have a flat shape with a
typediscriminator:{"type": "toolUseBlock", "name": "greet", "toolUseId": "tu_123", "input": {}}instead of the nested wrapper format that
contentBlockFromDataexpects:{"toolUse": {"name": "greet", "toolUseId": "tu_123", "input": {}}}This happens when blocks are spread (
{...block}) or when ORMs/storage layers flatten the nested structure.Solution
Added fallback handling in
contentBlockFromDatathat checks for thetypediscriminator when the nested wrapper keys aren't found. SupportstoolUseBlock,textBlock, andtoolResultBlock— the most common block types encountered in session storage.Tests
Added 3 tests verifying round-trip deserialization from type-discriminator format:
All 66 tests pass.