fix: serialize dict/list tool output to JSON (#6267)#6334
Conversation
There was a problem hiding this comment.
Summary: This PR changes tool-output formatting to serialize dict/list results as JSON in fallback paths.
Risk: Low risk. The change affects internal result serialization and does not introduce new authentication, authorization, data access, command execution, SQL, filesystem, or network attack surfaces; no exploitable security vulnerabilities were identified.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesStructured tool output formatting
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/crewai/src/crewai/tools/structured_tool.py (1)
75-87: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winWarning message no longer matches fallback behavior.
The warning says it falls back to
str(raw_result), but fordict/listit now falls back to JSON serialization. This is misleading during debugging.Proposed fix
- "Falling back to str(raw_result)." + "Falling back to JSON for dict/list, otherwise str(raw_result)."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/crewai/src/crewai/tools/structured_tool.py` around lines 75 - 87, The warning in StructuredTool’s fallback path is misleading because it always says “Falling back to str(raw_result)” even though dict/list values are serialized with json.dumps first. Update the warning text in structured_tool.py’s fallback block to accurately describe the actual behavior, referencing the structured tool result handling around the warning and the isinstance(raw_result, (dict, list)) branch so the message matches the returned value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/src/crewai/tools/structured_tool.py`:
- Around line 61-63: The fallback in structured_tool’s result formatting still
assumes dict/list values are JSON-serializable, so it can raise TypeError inside
the safe path. Update the logic around the raw_result handling in
structured_tool.py (and the same fallback near the other referenced block) to
use a serialization-safe approach or a guarded json.dumps call that cannot crash
on nested non-JSON-native objects, while preserving the existing dict/list vs.
scalar behavior.
---
Outside diff comments:
In `@lib/crewai/src/crewai/tools/structured_tool.py`:
- Around line 75-87: The warning in StructuredTool’s fallback path is misleading
because it always says “Falling back to str(raw_result)” even though dict/list
values are serialized with json.dumps first. Update the warning text in
structured_tool.py’s fallback block to accurately describe the actual behavior,
referencing the structured tool result handling around the warning and the
isinstance(raw_result, (dict, list)) branch so the message matches the returned
value.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 9ac6983f-ece5-4a44-ac08-fc562e42a367
📒 Files selected for processing (1)
lib/crewai/src/crewai/tools/structured_tool.py
- Add default=str to json.dumps calls to handle non-JSON-serializable objects - Update warning message to accurately describe fallback behavior (JSON for dict/list, otherwise str)
Fixes #6267
Summary
dictorlist,str()produces Python repr (e.g.{'key': 'value'}) instead of valid JSON. This causes parsing issues for agents expecting JSON.isinstance(raw_result, (dict, list))and usesjson.dumps()in both fallback paths of_format_tool_output_for_agent.Changes
lib/crewai/src/crewai/tools/structured_tool.py: In_format_tool_output_for_agent, usejson.dumps(raw_result)instead ofstr(raw_result)whenraw_resultis adictorlist(both in the no-schema early return and the validation-failure except fallback).Summary by CodeRabbit