Skip to content

refactor(tools): drop dead repair_json call in _validate_tool_input#6162

Open
i-anubhav-anand wants to merge 1 commit into
crewAIInc:mainfrom
i-anubhav-anand:refactor/tool-usage-dead-repair-call
Open

refactor(tools): drop dead repair_json call in _validate_tool_input#6162
i-anubhav-anand wants to merge 1 commit into
crewAIInc:mainfrom
i-anubhav-anand:refactor/tool-usage-dead-repair-call

Conversation

@i-anubhav-anand

@i-anubhav-anand i-anubhav-anand commented Jun 14, 2026

Copy link
Copy Markdown

Summary

In ToolUsage._validate_tool_input, the ast.literal_eval failure branch did:

try:
    arguments = ast.literal_eval(tool_input)
    if isinstance(arguments, dict):
        return arguments
except (ValueError, SyntaxError):
    repaired_input = repair_json(tool_input)   # assigned, never used

repaired_input is never read here — it is unconditionally reassigned a few lines later, where the actual repair is performed:

try:
    repaired_input = str(repair_json(tool_input, skip_json_loads=True))
    ...
    arguments = json.loads(repaired_input)

So the first repair_json(tool_input) call is a dead store that does redundant work and never affects the result.

Change

Replace the dead assignment with pass, matching the other except branches in the same function. No behavior change — the repair still happens via the existing json.loads/repair_json fallback.

Testing

  • uv run pytest tests/tools/test_tool_usage.py -k validate_tool_input15 passed (incl. test_validate_tool_input_invalid_json_repairable, which exercises the repair path).
  • ruff check / ruff format --check clean.

Summary by CodeRabbit

  • Refactor
    • Optimized tool input validation to streamline parsing and improve consistency in handling various input formats.

In the ast.literal_eval failure branch, `repaired_input = repair_json(tool_input)`
was assigned but never used — it is unconditionally reassigned (and the actual
repair performed) a few lines later in the json.loads/repair fallback. Removing
the dead store drops a redundant repair_json() call and matches the sibling
except branches (which simply `pass`). No behavior change.
@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4e451e04-70b7-4731-87b5-7277c9fc908f

📥 Commits

Reviewing files that changed from the base of the PR and between bb477f8 and 02cdba0.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/tools/tool_usage.py

📝 Walkthrough

Walkthrough

In ToolUsage._validate_tool_input, the intermediate repair_json(tool_input) assignment that ran after ast.literal_eval fails is removed. The parsing chain now moves directly from ast.literal_eval failure to json5.loads, while the later repair_json(..., skip_json_loads=True) step is unchanged.

Changes

JSON Parsing Fallback Adjustment

Layer / File(s) Summary
Remove early repair_json step
lib/crewai/src/crewai/tools/tool_usage.py
Deletes the repair_json(tool_input) intermediate assignment at line 878, so _validate_tool_input proceeds directly to json5.loads after ast.literal_eval fails.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

A rabbit hopped through parsing land,
One extra step was close at hand —
But repair_json in the middle? No more!
We skip straight to json5 like before.
Fewer hops, same burrow door. 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: removing a dead repair_json call from the _validate_tool_input method.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant