fix: handle BaseModel result in convert_to_model to prevent TypeError
Summary
Fixes #5458
When combining ConditionalTask + output_pydantic + guardrails + context, a guardrail retry can cause the agent to return a Pydantic BaseModel instance instead of a raw JSON string. This instance is then passed to convert_to_model(), which calls json.loads(result) on it — but json.loads expects str | bytes | bytearray, not a model object:
TypeError: the JSON object must be str, bytes or bytearray, not DrugClassificationOutput
Root Cause
In lib/crewai/src/crewai/utilities/converter.py, convert_to_model() assumes result is always a string (line 188: json.loads(result, strict=False)). When _invoke_guardrail_function in task.py retries via agent.execute_task() and the agent returns structured output, result is a BaseModel instance.
Fix
Added an early check in convert_to_model(): if result is already a BaseModel, serialize it to JSON with model_dump_json() before proceeding. This ensures json.loads always receives a string, regardless of whether the result came from a first-pass or retry execution.
The fix is minimal (6 lines) and does not change any behavior for string results.
Made with Cursor
Summary by CodeRabbit
- Chores
- Improved internal code documentation for model conversion handling to clarify behavior in edge cases.
This PR adds inline documentation to the BaseModel Serialization Clarification 🎯 1 (Trivial) | ⏱️ ~2 minutes 🐰 A comment, clear and bright, ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
convert_to_model function in the converter utility, explaining that when a BaseModel instance belongs to a different subclass (such as after a guardrail retry), it is serialized to JSON to allow downstream json.loads() and validation to proceed without triggering a TypeError.Changes
Layer / File(s)
Summary
JSON serialization documentation
lib/crewai/src/crewai/utilities/converter.pyComments explain why
BaseModel instances of different subclasses are serialized to JSON before downstream parsing, clarifying the behavior that prevents TypeError exceptions in mixed Pydantic model scenarios.Estimated code review effort
Poem
Explains the JSON serialization's might,
When models dance in different dress,
This note removes the guessing mess!🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title accurately describes the main change: handling BaseModel results in convert_to_model to prevent a TypeError.
Linked Issues check
✅ Passed
The PR successfully addresses issue
#5458 by adding a check to serialize BaseModel instances to JSON before calling json.loads(), preventing the TypeError.
Out of Scope Changes check
✅ Passed
The change is focused solely on fixing the identified issue in convert_to_model with an inline comment explaining the fix.
Docstring Coverage
✅ Passed
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
[!TIP]
💬 Introducing Slack Agent: The best way for teams to turn conversations into code.
Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
- Generate code and open pull requests
- Plan features and break down work
- Investigate incidents and troubleshoot customer tickets together
- Automate recurring tasks and respond to alerts with triggers
- Summarize progress and report instantly
Built for teams:
- Shared memory across your entire org—no repeating context
- Per-thread sandboxes to safely plan and execute work
- Governance built-in—scoped access, auditability, and budget controls
One agent for your entire SDLC. Right inside Slack.
Comment @coderabbitai help to get the list of available commands and usage tips.