Repository navigation
fix(pydantic-ai): trace harness background tool execution - #1196
Abhijeet Prasad (AbhiPrasad) merged 6 commits into
Conversation
|
I'll fix CI failures and address comments from users with write access that start with 'Devin'.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
There was a problem hiding this comment.
💡 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" |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
a766241 to
d265086
Compare
> 🤖 **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>
d265086 to
60d4446
Compare
Summary
Pydantic AI Harness background tools (
background_tools/_capability.py) schedule the real handler and return a pending receipt immediately. Ourtoolspan used to end at dispatch, recording only the receipt. Becausecontext.pyskips 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
toolspan 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:Agent.iter()now passes its root span to step persistence, so the lineage metadata from feat(pydantic-ai): trace step persistence lineage #1194 stays attached alongside the fix(pydantic-ai): trace direct agent iteration #1192 iter root span.Tests: success and failure regressions in
pydantic_ai/harness/test_background_tools.pyreplay real OpenAI VCR cassettes (harness/cassettes/latest/) in the Harness nox session from #1191.