Skip to content

Add include_binary_content to instrument_pydantic_ai #1090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions logfire/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ def instrument_pydantic_ai(
/,
*,
event_mode: Literal['attributes', 'logs'] = 'attributes',
include_binary_content: bool | None = None,
**kwargs: Any,
) -> None: ...

Expand All @@ -972,6 +973,7 @@ def instrument_pydantic_ai(
/,
*,
event_mode: Literal['attributes', 'logs'] = 'attributes',
include_binary_content: bool | None = None,
**kwargs: Any,
) -> pydantic_ai.models.Model: ...

Expand All @@ -981,6 +983,7 @@ def instrument_pydantic_ai(
/,
*,
event_mode: Literal['attributes', 'logs'] | None = None,
include_binary_content: bool | None = None,
**kwargs: Any,
) -> pydantic_ai.models.Model | None:
"""Instrument PydanticAI.
Expand All @@ -992,13 +995,19 @@ def instrument_pydantic_ai(
If you pass a model, a new instrumented model will be returned.
event_mode: See the [PydanticAI docs](https://ai.pydantic.dev/logfire/#data-format).
The default is whatever the default is in your version of PydanticAI.
include_binary_content: Whether to include base64 encoded binary content (e.g. images) in the events.
On by default. Requires PydanticAI 0.2.5 or newer.
kwargs: Additional keyword arguments to pass to
[`InstrumentationSettings`](https://ai.pydantic.dev/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings)
for future compatibility.
"""
from .integrations.pydantic_ai import instrument_pydantic_ai

self._warn_if_not_initialized_for_instrumentation()

if include_binary_content is not None:
kwargs['include_binary_content'] = include_binary_content

return instrument_pydantic_ai(
self,
obj=obj,
Expand Down
4 changes: 3 additions & 1 deletion tests/otel_integrations/test_pydantic_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ def get_model(a: Agent):
assert m2 is model

# Now instrument all agents. Also use the (currently not default) event mode.
logfire_inst.instrument_pydantic_ai(event_mode='logs')
logfire_inst.instrument_pydantic_ai(event_mode='logs', include_binary_content=False)
m = get_model(agent1)
assert isinstance(m, InstrumentedModel)
# agent1 still has its own instrumentation settings which override the global ones.
assert m.settings.event_mode == InstrumentationSettings().event_mode == 'attributes'
assert m.settings.include_binary_content == InstrumentationSettings().include_binary_content
# agent2 uses the global settings.
m2 = get_model(agent2)
assert isinstance(m2, InstrumentedModel)
assert m2.settings.event_mode == 'logs'
assert not m2.settings.include_binary_content

# Remove the global instrumentation. agent1 remains instrumented.
Agent.instrument_all(False)
Expand Down
Loading