Skip to content

fix: handle type-discriminator format in contentBlockFromData#787

Open
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
kai-agent-free:fix/content-block-type-fallback
Open

fix: handle type-discriminator format in contentBlockFromData#787
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
kai-agent-free:fix/content-block-type-fallback

Conversation

@kai-agent-free
Copy link
Copy Markdown

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 type discriminator:

{"type": "toolUseBlock", "name": "greet", "toolUseId": "tu_123", "input": {}}

instead of the nested wrapper format that contentBlockFromData expects:

{"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 contentBlockFromData that checks for the type discriminator when the nested wrapper keys aren't found. Supports toolUseBlock, textBlock, and toolResultBlock — the most common block types encountered in session storage.

Tests

Added 3 tests verifying round-trip deserialization from type-discriminator format:

  • ToolUseBlock with type discriminator
  • TextBlock with type discriminator
  • ToolResultBlock with type discriminator

All 66 tests pass.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ToolUse not picked up correctly

1 participant