Skip to content

Conversation

Ratish1
Copy link
Contributor

@Ratish1 Ratish1 commented Oct 3, 2025

Description

This PR fixes a KeyError that occurred when a text field was a plain string instead of a dictionary. Previously we assumed guardContent["text"] was always a dict which caused TypeError /KeyError in some payloads.

This change:

  • Accepts guardContent.text as either str or dict.
  • Preserves qualifiers when present in the dict form.
  • Adds unit tests for the three scenarios: guardContent.text as dict-with-qualifiers, dict-without-qualifiers, and plain string.

Related Issues

Fixes #959

Documentation PR

N/A

Type of Change

Bug fix

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
  • Pre-commit hooks, formatting and linting passed locally
  • I ran unit tests for bedrock:
  • pytest -q tests/strands/models/test_bedrock.py::test_format_request_guard_content_with_qualifiers
  • pytest -q tests/strands/models/test_bedrock.py::test_format_request_guard_content_without_qualifiers
  • pytest -q tests/strands/models/test_bedrock.py::test_format_request_guard_content_text_as_string

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.

@VANDRANKI
Copy link

VANDRANKI commented Oct 3, 2025

Outstanding work, @Ratish1! 🎉

This is an exemplary PR that perfectly demonstrates production-grade engineering. Let me highlight what makes this contribution so valuable:

🔧 Elegant Problem Solving

Your approach to handling both string and dict types for guardContent.text is exactly the kind of defensive programming that prevents production incidents. The fact that you're preserving qualifiers when present in dict form shows deep understanding of backward compatibility, this will seamlessly support all existing use cases while gracefully handling edge cases that previously caused KeyErrors.

Exceptional Testing Coverage

The three comprehensive test scenarios you've added (dict-with-qualifiers, dict-without-qualifiers, and string) are a masterclass in thorough testing. This level of coverage not only proves your fix works but also creates a safety net that prevents future regressions. Future maintainers will thank you for this!

📋 Professional Execution

Your adherence to the contribution checklist is impressive:

  • ✓ Pre-commit hooks passing
  • ✓ Local linting and formatting complete
  • ✓ Unit tests verified and running
  • ✓ Clear PR description with context

This makes review effortless and demonstrates respect for the team's processes.

🚀 Real Impact

This fix directly addresses issue #959 and will immediately improve resilience for all downstream consumers (agents-docs, agents-tools, agents-cli). By enhancing input type flexibility, you're preventing runtime failures in production environments—that's huge value!

💡 Minor Suggestion

Since documentation updates are pending, consider adding a brief note or example in future docs clarifying that guardContent.text now accepts both formats. This will help users understand the flexibility and may prevent confusion.

🌟 Final Thoughts

Contributions like this are what make open source projects thrive. You've not only fixed a bug but elevated the SDK's reliability and robustness. The community and all users of this SDK will benefit from your careful, thoughtful work.

Thank you for making strands-agents better! Looking forward to seeing this merged. 🚢

VANDRANKI

This comment was marked as resolved.

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] KeyError: 'qualifiers' with 1.5.0 up to 1.10.0

2 participants