Skip to content

Commit 31999e9

Browse files
committed
Some documentation for span_attributes.
1 parent a64dcc2 commit 31999e9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/logfire.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,38 @@ Agent.instrument_all(instrumentation_settings)
356356
```
357357

358358
This setting is particularly useful in production environments where compliance requirements or data sensitivity concerns make it necessary to limit what content is sent to your observability platform.
359+
360+
### Adding custom attributes to agent spans
361+
362+
If you want to add metadata to and agent span, pass the [`span_attributes`][pydantic_ai.models.instrumented.InstrumentationSettings] argument.
363+
You can either provide a static `dict[str, str]`:
364+
365+
```python {title="instrumentation_span_attributes_static.py"}
366+
from pydantic_ai import Agent
367+
from pydantic_ai.models.instrumented import InstrumentationSettings
368+
369+
agent = Agent(
370+
'openai:gpt-5',
371+
instrument=InstrumentationSettings(
372+
span_attributes={
373+
'gen_ai.agent.description': 'A fiery agent!',
374+
'my_org.tenant.id': '42',
375+
}
376+
)
377+
)
378+
```
379+
380+
Alternatively, supply a callable that receives the run [`RunContext`][pydantic_ai.tools.RunContext] so attributes can be derived from the prompt, dependencies, or usage data collected during the run:
381+
382+
```python {title="instrumentation_span_attributes_callable.py"}
383+
from pydantic_ai import Agent, RunContext
384+
from pydantic_ai.models.instrumented import InstrumentationSettings
385+
386+
def span_attribute_callback(ctx: RunContext[None]) -> dict[str, str]:
387+
return {'my_org.prompt_preview': str(ctx.prompt)[:100]}
388+
389+
agent = Agent(
390+
'openai:gpt-5-nano',
391+
instrument=InstrumentationSettings(span_attributes=span_attribute_callback)
392+
)
393+
```

0 commit comments

Comments
 (0)