Skip to content

perf(pydantic_ai): simplify trace shaping#357

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intomainfrom
abhi-refactor-pydantic-ai-trace-shaping
Apr 28, 2026
Merged

perf(pydantic_ai): simplify trace shaping#357
Abhijeet Prasad (AbhiPrasad) merged 1 commit intomainfrom
abhi-refactor-pydantic-ai-trace-shaping

Conversation

@AbhiPrasad
Copy link
Copy Markdown
Member

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Apr 27, 2026

Avoid pre-serializing Pydantic AI values in tracing helpers. Keep integration-side shaping focused on readable span payloads and materializing binary content into Braintrust attachments, while leaving general serialization to Braintrust logging.

Update private helper names and tests from serialize_* to shape_* to reflect the narrower responsibility.

I did a quick local microbenchmark against an approximation of the previous helper behavior using the pydantic-ai latest nox env.

 Results:

 ┌────────────────────────────────────┬──────────┬─────────┬────────────────────────┐
 │ case                               │ old      │ new     │ speedup                │
 ├────────────────────────────────────┼──────────┼─────────┼────────────────────────┤
 │ binary message shaping             │ 24.83 µs │ 5.45 µs │ 4.6x                   │
 ├────────────────────────────────────┼──────────┼─────────┼────────────────────────┤
 │ text messages x5                   │ 51.79 µs │ 7.31 µs │ 7.1x                   │
 ├────────────────────────────────────┼──────────┼─────────┼────────────────────────┤
 │ nested binary content part         │ 15.01 µs │ 4.86 µs │ 3.1x                   │
 ├────────────────────────────────────┼──────────┼─────────┼────────────────────────┤
 │ model response text                │ 13.84 µs │ 1.94 µs │ 7.1x                   │
 ├────────────────────────────────────┼──────────┼─────────┼────────────────────────┤
 │ model settings copy vs passthrough │ 0.79 µs  │ ~0 µs   │ effectively eliminated │
 └────────────────────────────────────┴──────────┴─────────┴────────────────────────┘

Avoid pre-serializing Pydantic AI values in tracing helpers. Keep integration-side shaping focused on readable span payloads and materializing binary content into Braintrust attachments, while leaving general serialization to Braintrust logging.

Update private helper names and tests from serialize_* to shape_* to reflect the narrower responsibility.
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) changed the title ref(pydantic_ai): simplify trace shaping perf(pydantic_ai): simplify trace shaping Apr 27, 2026
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) merged commit cf07dab into main Apr 28, 2026
82 checks passed
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) deleted the abhi-refactor-pydantic-ai-trace-shaping branch April 28, 2026 17:19
Abhijeet Prasad (AbhiPrasad) added a commit that referenced this pull request Apr 29, 2026
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