Skip to content

Conversation

@strands-agent
Copy link
Contributor

Description

Fixes #1485 - Cannot use structured_output with thinking enabled.

Supersedes #1490 (cleaner implementation rebased on latest main after #1336 pyupgrade merge)

Problem

When using BedrockModel with thinking mode enabled via additional_request_fields, if structured_output fails to generate a tool call on the first attempt, the event loop forces tool_choice (set to {"any": {}}) to retry. However, Bedrock's API doesn't allow thinking mode to be enabled when tool_choice forces tool use, resulting in:

ValidationException: The model returned the following errors: Thinking may not be enabled when tool_choice forces tool use.

Solution

Adds a helper method _get_additional_request_fields() that temporarily removes the thinking configuration from additionalModelRequestFields when tool_choice is forcing tool use (i.e., not {"auto": {}}).

Key changes:

  1. Detect when tool_choice is forcing a tool (not auto and not None)
  2. Create a copy of additional_request_fields without the thinking key when forcing
  3. Preserve thinking mode for all normal agent interactions (when tool_choice is auto or None)
  4. Preserve all other additional_request_fields when removing thinking

Testing

Added 7 comprehensive unit tests in test_bedrock_thinking.py covering:

  • ✅ Thinking removed when forcing tool use ({"any": {}})
  • ✅ Thinking removed when forcing specific tool ({"tool": {...}})
  • ✅ Thinking preserved with auto tool choice ({"auto": {}})
  • ✅ Thinking preserved when no tool choice (None)
  • ✅ Other additional fields preserved when thinking is removed
  • ✅ Models without thinking config work normally
  • ✅ Fields without thinking preserved when forcing tool

Behavior

Before: Agent crashes with ValidationException when structured_output + thinking enabled
After: Agent gracefully retries structured_output by temporarily disabling thinking for that specific retry

Related Issues

Fixes #1485
Supersedes #1490

Type of Change

  • Bug fix

Testing

  • hatch test tests/strands/models/test_bedrock_thinking.py - 7 passed
  • hatch test tests/strands/models/test_bedrock.py - 94 passed
  • hatch fmt --linter - All checks passed

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective
  • My changes generate no new warnings
  • This is a non-breaking change (thinking mode still works for normal interactions)

@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

strands-agent and others added 2 commits January 15, 2026 15:41
Fixes strands-agents#1485 - Cannot use structured_output with thinking enabled.

When BedrockModel has thinking mode enabled via additional_request_fields,
and the event loop forces tool_choice (for structured_output retry),
Bedrock's API returns an error because thinking is not allowed with
forced tool use.

This PR adds a helper method _get_additional_request_fields() that
temporarily removes the thinking configuration when tool_choice is
forcing tool use (not 'auto' and not None), while preserving all
other additional request fields.

- Adds _get_additional_request_fields() helper method
- Preserves thinking for normal interactions (auto/None tool_choice)
- Removes thinking only when forcing tool use
- Preserves other additional_request_fields when thinking is removed
- Adds comprehensive unit tests for all scenarios
dbschmigelski
dbschmigelski previously approved these changes Jan 15, 2026
mkmeral
mkmeral previously approved these changes Jan 15, 2026
@dbschmigelski dbschmigelski dismissed stale reviews from mkmeral and themself via 280a7da January 15, 2026 21:44
@github-actions github-actions bot removed the size/m label Jan 15, 2026
@dbschmigelski dbschmigelski requested a review from mkmeral January 15, 2026 21:58
@dbschmigelski dbschmigelski merged commit bce2464 into strands-agents:main Jan 16, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Cannot use structured_output with thinking enabled

4 participants