Skip to content

fix(pydantic-ai): trace harness background tool execution - #1196

Merged
Abhijeet Prasad (AbhiPrasad) merged 6 commits into
mainfrom
devin/1795708389-pydantic-ai-background-tools-tracing
Nov 26, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 6 commits into
mainfrom
devin/1795708389-pydantic-ai-background-tools-tracing

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Pydantic AI Harness background tools (background_tools/_capability.py) schedule the real handler and return a pending receipt immediately. Our tool span used to end at dispatch, recording only the receipt. Because context.py skips ended spans, the later child agent run attached directly to the parent agent.

An optional Harness patcher, skipped on pydantic-ai 1.x, now produces ONE tool span per model tool call, following the instrumentation spec. That span covers the actual run-scoped handler: it records the model-supplied arguments and the handler's real return value or error. The handler runs with that span current:

before: agent -> tool (pending receipt)      agent -> child agent run
after:  agent -> tool (args, real result/error) -> child agent run

Tests: success and failure regressions in pydantic_ai/harness/test_background_tools.py replay real OpenAI VCR cassettes (harness/cassettes/latest/) in the Harness nox session from #1191.

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:04:02.400591Z d876f61 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d876f619dc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".

target_module = _BACKGROUND_TOOLS_CAPABILITY_MODULE
target_path = "BackgroundTools.wrap_tool_execute"
wrapper = _background_tool_execution_wrapper
min_version: ClassVar[str] = "2.0.0"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Gate the patcher with version_spec

When pydantic_ai_harness is importable alongside Pydantic AI 1.x, this intended 2.x gate is ignored because BasePatcher.applies() consults version_spec, not min_version. The patcher can therefore wrap a target whose calling contract predates the implementation this wrapper assumes; use version_spec = ">=2.0.0" (or an explicit applies() feature gate) so the supported 1.107.9 matrix remains unmodified.

AGENTS.md reference: AGENTS.md:L138-L145

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: BackgroundToolExecutionPatcher now uses version_spec = ">=2.0.0", and the Harness regression asserts it does not apply on pydantic-ai 1.107.9 even when the hook targets exist.

Written by Devin

@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/1795708389-pydantic-ai-background-tools-tracing branch 4 times, most recently from a766241 to d265086 Compare November 26, 2026 16:51
> 🤖 **This text was AI-generated.**

A Harness background tool returns a pending receipt before the handler finishes.
That receipt remains on the dispatch span; previously the handler’s result and
its child agent runs had no execution span.

Wrap the scheduled handler so it records its result or error in a Braintrust
tool span with the dispatch as its explicit parent.

```text
agent run -> tool dispatch -> tool execution -> child agent run
```

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
> 🤖 **This text was AI-generated.**

Run the background tool regressions with the same offline logger setup and
agent builder as the other Harness tests. Keep the custom model response
that synchronizes on the pending receipt.

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
> 🤖 **This text was AI-generated.**

Use recorded OpenAI calls to dispatch the Harness background tool and run
its child agent. Keep the successful result and the tool-raised error in
separate cassettes so both trace shapes can be checked without live API access.

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
> 🤖 **This text was AI-generated.**

A Harness background tool returned a pending receipt to the model and
produced two Braintrust tool spans. Trace the run-scoped handler instead
so one tool span contains the model arguments and the real result or error.
The model still receives the receipt.

```text
agent run -> tool execution -> child agent run
```

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
> 🤖 **This text was AI-generated.**

Step persistence needs the current agent span to attach lineage. Pass
the span from direct iteration so the trace opens and receives lineage
metadata. Replay the background tool scenario through iter as well to
cover the child agent nesting under its tool span.

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
> 🤖 **This text was AI-generated.**

The patcher looked like it required Pydantic AI 2.x, but the patcher
base class does not consult min_version. Use version_spec so 1.x skips
the Harness patch even when both hook targets can be resolved.

Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/1795708389-pydantic-ai-background-tools-tracing branch from d265086 to 60d4446 Compare November 26, 2026 17:05
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit 825f457 into main Nov 26, 2026
98 checks passed
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) deleted the devin/1795708389-pydantic-ai-background-tools-tracing branch November 26, 2026 17:15
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