Skip to content

fix(pydantic-ai): don't log deferred tool calls as tool span errors - #969

Closed
Ethan Leifer (ethanleifer) wants to merge 1 commit into
braintrustdata:mainfrom
ethanleifer:fix/pydantic-ai-deferred-tool-spans
Closed

Ethan Leifer (ethanleifer) wants to merge 1 commit into
braintrustdata:mainfrom
ethanleifer:fix/pydantic-ai-deferred-tool-spans

Conversation

@ethanleifer

Copy link
Copy Markdown

Problem

pydantic_ai tools can raise CallDeferred or ApprovalRequired to hand a call back to the caller for external execution or human approval. These exceptions are control flow, and the agent run ends normally with DeferredToolRequests.

_trace_tool_execution opens the tool span as a with block, so these exceptions leave the span and get recorded as its error. Every deferred or approval-gated tool call therefore shows up as a failed tool in traces. We hit this in production on a tool that defers to the UI for user confirmation.

Fix

  • Inside the tool span, catch CallDeferred and ApprovalRequired and log {"deferred": true, "reason": "<exception name>"} as the span output, along with the usual timing metrics.
  • Re-raise the same exception after the span closes, so pydantic_ai's deferral handling is unchanged.
  • All other exceptions still propagate through the span and are recorded as errors.
  • The exception classes are imported lazily behind an ImportError guard, because older pydantic_ai versions (for example 0.1.9 in the wrap-openai matrix) don't define them.

Testing

Added tests to test_pydantic_ai_integration.py:

  • test_deferred_tool_call_is_not_logged_as_error, parametrized over CallDeferred and ApprovalRequired. It checks the run returns DeferredToolRequests and that the tool span has no error and a deferred output. Both cases fail on main and pass with this change.
  • test_tool_exception_is_logged_as_error checks that a RuntimeError from a tool is still recorded on the span.

These tests drive the agent with pydantic_ai's FunctionModel instead of a VCR cassette. The behaviour under test comes from the local tool raising an exception, not from the shape of a provider response.

Sessions run locally, all passing:

  • test_pydantic_ai_integration(latest) and (1.10.0)
  • test_pydantic_ai_wrap_openai(latest), (1.0.1) and (0.1.9)
  • test_pydantic_ai_logfire(latest)
  • pylint and pre-commit on the changed files

🤖 Generated with Claude Code

pydantic_ai tools raise CallDeferred or ApprovalRequired to hand a call
back to the caller for external execution or approval. The tool span
recorded these exceptions as errors, so every deferred call looked like
a failed tool in traces.

Log these deferrals as the span output and re-raise them after the span
closes. Other exceptions are still recorded as errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T09:56:58.216963Z e8a6b8f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AbhiPrasad

Copy link
Copy Markdown
Member

opened #975 to supercede this PR, closing this PR out

Abhijeet Prasad (AbhiPrasad) added a commit that referenced this pull request Oct 19, 2026
Fixes https://linear.app/braintrustdata/issue/SDK-581

Supersedes #969 (thanks to @ethanleifer for the PR) 

## AI Summary

Pydantic AI tools that raise `CallDeferred` or `ApprovalRequired` were
logged as **failed** tool spans, even though these exceptions are
Pydantic AI's normal way to defer a call or request human approval. Real
failures show up as errors on the tool span, so deferred calls were
tripping monitoring and alerting.

## How this differs from #969

| | #969 | This PR |
| --- | --- | --- |
| Where the deferral is recorded | `output={"deferred": True, "reason":
"<ExceptionName>"}` | `metadata.deferred` (+ `deferred_metadata`) |
| Fallback for Pydantic AI without these exceptions | `ImportError`
fallback | not needed: the integration's `min_version` is `1.10.0`,
which has both |
| Reference to the re-raised exception | kept | cleared after re-raise,
to avoid an exception → traceback → frame reference cycle |

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants