← Back to Pull requests

fix(crewai): trace tools run by native function calling #292

Closed
unknown closed this pull request without merging on Nov 20, 2026 01:31 UTC
View on GitHub
unknown unknown commented on Oct 28, 2026 11:20 UTC
Author

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 through ToolUsage._use. The instrumentation only wrapped ToolUsage._use, so these tools ran without a TOOL span.

This PR also wraps BaseTool.run, and Tool.run (the class behind @tool, which overrides it), with the same execute_tool span: same name, attributes and parent (the AGENT span) as on the ReAct path. ToolUsage._use reaches the tool through its CrewStructuredTool and _run, not run. Even so, a run of the same tool inside a traced _use is 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

  • Bug fix (non-breaking change which fixes an issue)
  • How Has This Been Tested?

  • New tests/test_native_tool_calls.py, with the OpenAI-compatible API mocked by respx, so it needs no cassette and runs with block_network. It covers one tool call, a failing tool (ERROR status), two parallel calls (both under the AGENT span), no second span for run inside a traced _use, and uninstrumentation. Without the fix, 4 of the 5 fail; the duplicate-span test fails when the guard is removed.

  • The spans of the 41 existing span-checking tests (name, kind, operation, status, parent) are unchanged on crewai 1.15.30 and 1.15.32. The native TOOL span has the same attribute keys and values as the ReAct one for the same tool call.

  • 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 check and ruff format --check pass, and so does tox -e precommit (Python 3.11).
  • Does This PR Require a Core Repo Change?

  • No.
  • Checklist:

  • Followed the style guidelines of this project

  • Changelogs have been updated

  • Unit tests have been added

  • Documentation has been updated
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

    Diff: 305 lines (+228 / -77) across 3 files Reviewer: pylaterreur Review duration: 32530m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.8% · Safe to merge
    17.8%