fix(core): ensure tool_choice maps to structured object for AWS Bedrock compliance#668
Conversation
Greptile SummaryThis PR normalizes
Confidence Score: 4/5The Bedrock request path still has cases that can send the raw string, and explicit tool selections can be changed to auto.
strix/core/execution.py Important Files Changed
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
strix/core/execution.py:354-358
**Top-Level Tool Choice Skips Normalization**
When `extra_body` is a dict without its own `tool_choice`, this `elif` skips the top-level `run_config.tool_choice` branch. A Bedrock run with `extra_body == {}` and `tool_choice == "auto"` can still reach `Runner.run_streamed` with the raw string and fail with the same `tool_choice.type` validation error.
```suggestion
if hasattr(run_config, "extra_body") and isinstance(run_config.extra_body, dict):
if "tool_choice" in run_config.extra_body:
run_config.extra_body["tool_choice"] = {"type": "auto"}
if hasattr(run_config, "tool_choice") and (run_config.tool_choice == "auto" or isinstance(run_config.tool_choice, str)):
run_config.tool_choice = {"type": "auto"}
```
### Issue 2 of 2
strix/core/execution.py:357-358
**Explicit Tool Choice Becomes Auto**
The `isinstance(run_config.tool_choice, str)` branch rewrites every string value to `{"type": "auto"}`. If a caller selects a specific tool by name, the request is changed to automatic selection before `Runner.run_streamed`, so the model can skip the required tool or call a different one.
```suggestion
elif hasattr(run_config, "tool_choice") and run_config.tool_choice == "auto":
run_config.tool_choice = {"type": "auto"}
```
Reviews (1): Last reviewed commit: "fix(core): ensure tool_choice maps to st..." | Re-trigger Greptile |
| elif hasattr(run_config, "tool_choice") and (run_config.tool_choice == "auto" or isinstance(run_config.tool_choice, str)): | ||
| run_config.tool_choice = {"type": "auto"} |
There was a problem hiding this comment.
Explicit Tool Choice Becomes Auto
The isinstance(run_config.tool_choice, str) branch rewrites every string value to {"type": "auto"}. If a caller selects a specific tool by name, the request is changed to automatic selection before Runner.run_streamed, so the model can skip the required tool or call a different one.
| elif hasattr(run_config, "tool_choice") and (run_config.tool_choice == "auto" or isinstance(run_config.tool_choice, str)): | |
| run_config.tool_choice = {"type": "auto"} | |
| elif hasattr(run_config, "tool_choice") and run_config.tool_choice == "auto": | |
| run_config.tool_choice = {"type": "auto"} |
Prompt To Fix With AI
This is a comment left during a code review.
Path: strix/core/execution.py
Line: 357-358
Comment:
**Explicit Tool Choice Becomes Auto**
The `isinstance(run_config.tool_choice, str)` branch rewrites every string value to `{"type": "auto"}`. If a caller selects a specific tool by name, the request is changed to automatic selection before `Runner.run_streamed`, so the model can skip the required tool or call a different one.
```suggestion
elif hasattr(run_config, "tool_choice") and run_config.tool_choice == "auto":
run_config.tool_choice = {"type": "auto"}
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Description
Ensures that
tool_choiceis formatted as a structured object ({"type": "auto"}) rather than a raw string when communicating via LiteLLM/Bedrock. This fixes an API validation failure where AWS Bedrock rejects requests with a 400 Bad Request.Error Log
litellm.exceptions.BadRequestError: BedrockException - {"message":"The model returned the following errors: tool_choice.type: Field required"}