Skip to content

Conversation

dbschmigelski
Copy link
Member

@dbschmigelski dbschmigelski commented Sep 10, 2025

Description

This PR fixes a problem with AWS Bedrock's strict API validation. Bedrock throws validation exceptions when it receives unexpected fields in content blocks, unlike other model providers that ignore unknown fields. This means we need to filter all content blocks before sending them to Bedrock.

The current implementation only handles tool result blocks with hardcoded filtering. But Bedrock supports many content block types (image, document, video, reasoning, citations, cache points, guard content, etc.), each with different field requirements. The hardcoded approach doesn't scale as we add support for more content types.

This Refactor decouples the Strands ContentType and the Bedrock Model

The original implementation only handled filtering at the top level. After a comment I began investigating filtering deeply nested objects. Where this broke down was the inability to model recursive structures using the key filtering technique.

So what is introduced here is effectively the same strategy we use across all other providers. We provide a content mapping from the Strands ContentTypes to the model provider. No longer is Bedrock special cased. This will allow us to grow the content types as we wish independent of the underlying bedrock impl. We still can, and should, use the Bedrock shapes as our inspiration for new features but we are no longer as tightly coupled.

For a different approach that I may want to consider in the future, I believe it may be useful to simply create _Bedrock{}ContentType shapes in Pydantic. Then in a single call we just do _BedrockContent.model_validate(input) where Pydantic will handle this; the same strategy can be used across different providers and may clean up our code.

Related Issues

#341
#343

Documentation PR

N/A

Type of Change

Chore to allow future change

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dbschmigelski dbschmigelski changed the title chore: filter all ContentBlock fields in BedrockModel chore: validate all ContentBlock fields in BedrockModel Sep 10, 2025
@dbschmigelski dbschmigelski marked this pull request as ready for review September 10, 2025 17:52
@dbschmigelski dbschmigelski changed the title chore: validate all ContentBlock fields in BedrockModel chore: decouple Strands ContentBlock and BedrockModel Sep 15, 2025
@dbschmigelski dbschmigelski changed the title chore: decouple Strands ContentBlock and BedrockModel feat: decouple Strands ContentBlock and BedrockModel Sep 15, 2025
@dbschmigelski dbschmigelski enabled auto-merge (squash) September 18, 2025 13:08
@dbschmigelski dbschmigelski merged commit 406458d into strands-agents:main Sep 18, 2025
11 of 12 checks passed
@Unshure
Copy link
Member

Unshure commented Sep 18, 2025

Created a new issue to add support for generic content blocks for better extensibility: #889

@Unshure Unshure mentioned this pull request Sep 19, 2025
7 tasks
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.

3 participants