fix(crewai): trace tools run by native function calling #292
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 305 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.342
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #292"
general
weight: 0.35
pylaterreur
Description
When an agent's LLM supports function calling, as crewAI's native providers do, crewAI runs the tool calls returned by the model through
BaseTool.run(AgentExecutor._execute_single_native_tool_call), not throughToolUsage._use. The instrumentation only wrappedToolUsage._use, so these tools ran without a TOOL span.This PR also wraps
BaseTool.run, andTool.run(the class behind@tool, which overrides it), with the sameexecute_toolspan: same name, attributes and parent (the AGENT span) as on the ReAct path.ToolUsage._usereaches the tool through itsCrewStructuredTooland_run, notrun. Even so, arunof the same tool inside a traced_useis not traced again, so a tool call never gets two spans. Parallel tool calls run in copied contexts and keep the AGENT span as parent.Fixes #291
Type of change
How Has This Been Tested?
tests/test_native_tool_calls.py, with the OpenAI-compatible API mocked by respx, so it needs no cassette and runs withblock_network. It covers one tool call, a failing tool (ERROR status), two parallel calls (both under the AGENT span), no second span forruninside a traced_use, and uninstrumentation. Without the fix, 4 of the 5 fail; the duplicate-span test fails when the guard is removed.tox -c tox-loongsuite.ini -e py3{10,11,12,13}-test-loongsuite-instrumentation-crewai(tox-uv 1.35.2, GitHub CPython builds), together with #290, which the existing tests need on crewai 1.15.31 and later: 53 passed on each version with crewai 1.15.32, online and offline (bwrap --unshare-net), and offline with crewai 1.15.30. Without #290, on crewai 1.15.30 (litellm 1.95.0), Python 3.12: 53 passed.ruff checkandruff format --checkpass, and so doestox -e precommit(Python 3.11).Does This PR Require a Core Repo Change?
Checklist:
🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN