Python · Instrumentation
openinference-instrumentation-pydantic-ai
OpenInference PydanticAI Instrumentation
- Module
openinference.instrumentation.pydantic_ai- Version
- 0.1.37
- Python
- >=3.10, <3.16
- Instruments
pydantic-ai>=2.24.0
Install
pip install openinference-instrumentation-pydantic-aiopeninference.instrumentation.pydantic_ai
OpenInferenceSpanProcessor
classSource ↗class OpenInferenceSpanProcessor(
span_filter: Optional[SpanFilter] = None,
)Also importable from openinference.instrumentation.pydantic_ai.span_processor.
A standalone span processor that enhances spans with OpenInference attributes.
This processor processes spans by enhancing them with OpenInference attributes and can optionally filter which spans are processed based on a filter function. Unlike the Simple and Batch processors, this processor only modifies span attributes in-place without exporting them - it’s designed to work alongside other processors.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
span_filter | Optional[SpanFilter] | None | Optional filter function to determine if a span should be processed |
Example
from openinference.instrumentation.pydantic_ai import OpenInferenceSpanProcessor
from openinference.instrumentation.pydantic_ai.utils import is_openinference_span
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Add the OpenInference processor to enhance spans
openinference_processor = OpenInferenceSpanProcessor(
span_filter=is_openinference_span
)
tracer_provider.add_span_processor(openinference_processor)
# Add a separate exporter processor
exporter = OTLPSpanExporter()
export_processor = SimpleSpanProcessor(exporter)
tracer_provider.add_span_processor(export_processor)on_start
methodSource ↗def on_start(
span: Span,
parent_context: Optional[Context] = None,
) -> NoneCalled when a span starts. No processing needed at start.
on_end
methodSource ↗def on_end(span: ReadableSpan) -> NoneProcess a span when it ends by enhancing it with OpenInference attributes.
shutdown
methodSource ↗def shutdown() -> NoneShutdown the processor. No cleanup needed.
force_flush
methodSource ↗def force_flush(timeout_millis: int = 30000) -> boolForce flush any pending operations. Always returns True as there’s nothing to flush.
is_openinference_span
functionSource ↗def is_openinference_span(span: ReadableSpan) -> boolAlso importable from openinference.instrumentation.pydantic_ai.utils.
Check if a span is an OpenInference span.
openinference.instrumentation.pydantic_ai.utils
should_export_span
functionSource ↗def should_export_span(
span: ReadableSpan,
span_filter: Optional[SpanFilter] = None,
) -> boolDetermine if a span should be exported based on a filter.
openinference.instrumentation.pydantic_ai.semantic_conventions
OTELConventions
classSource ↗class OTELConventionsAttributes
| Name | Type and value |
|---|---|
EVENTS | 'events' |
EVENT_NAME | 'event.name' |
GenAIConversationID
classSource ↗class GenAIConversationIDAttributes
| Name | Type and value |
|---|---|
CONVERSATION_ID | 'gen_ai.conversation.id' |
GenAIEventNames
classSource ↗class GenAIEventNamesAttributes
| Name | Type and value |
|---|---|
SYSTEM_MESSAGE | 'gen_ai.system.message' |
USER_MESSAGE | 'gen_ai.user.message' |
ASSISTANT_MESSAGE | 'gen_ai.assistant.message' |
TOOL_MESSAGE | 'gen_ai.tool.message' |
CHOICE | 'gen_ai.choice' |
GenAIMessageFields
classSource ↗class GenAIMessageFieldsAttributes
| Name | Type and value |
|---|---|
ID | 'id' |
CONTENT | 'content' |
ROLE | 'role' |
PARTS | 'parts' |
TOOL_CALLS | 'tool_calls' |
GenAIMessageRoles
classSource ↗class GenAIMessageRolesGenAIMessagePartFields
classSource ↗class GenAIMessagePartFieldsGenAIMessagePartTypes
classSource ↗class GenAIMessagePartTypesAttributes
| Name | Type and value |
|---|---|
TEXT | 'text' |
TOOL_CALL | 'tool_call' |
TOOL_CALL_RESPONSE | 'tool_call_response' |
GenAIToolCallFields
classSource ↗class GenAIToolCallFieldsGenAIFunctionFields
classSource ↗class GenAIFunctionFieldsGenAIChoiceBodyFields
classSource ↗class GenAIChoiceBodyFieldsAttributes
| Name | Type and value |
|---|---|
INDEX | 'index' |
FINISH_REASON | 'finish_reason' |
MESSAGE | 'message' |
TOOL_CALLS | 'tool_calls' |
GenAISystemInstructionsFields
classSource ↗class GenAISystemInstructionsFieldsPydanticAgentName
classSource ↗class PydanticAgentNameAttributes
| Name | Type and value |
|---|---|
AGENT | 'agent_name' |
PydanticTools
classSource ↗class PydanticToolsAttributes
| Name | Type and value |
|---|---|
TOOLS | 'tools' |
TOOL_ARGUMENTS | 'tool_arguments' |
TOOL_RESPONSE | 'tool_response' |
PydanticGenAIAttribute
classSource ↗class PydanticGenAIAttributeAttributes
| Name | Type and value |
|---|---|
GEN_AI | 'gen_ai' |
PydanticGenAITool
classSource ↗class PydanticGenAIToolAttributes
| Name | Type and value |
|---|---|
TOOL | 'tool' |
PydanticCustomAttributes
classSource ↗class PydanticCustomAttributesAttributes
| Name | Type and value |
|---|---|
MODEL_REQUEST_PARAMETERS | 'model_request_parameters' |
PydanticModelRequestParameters
classSource ↗class PydanticModelRequestParametersAttributes
| Name | Type and value |
|---|---|
TOOLS | 'output_tools' |
FUNCTION_TOOLS | 'function_tools' |
NAME | 'name' |
DESCRIPTION | 'description' |
PARAMETERS | 'parameters' |
PydanticModelRequestParametersTool
classSource ↗class PydanticModelRequestParametersToolAttributes
| Name | Type and value |
|---|---|
NAME | 'name' |
DESCRIPTION | 'description' |
PARAMETERS_JSON_SCHEMA | 'parameters_json_schema' |
PARAMETERS | 'properties' |
PydanticMessageRoleUser
classSource ↗class PydanticMessageRoleUserAttributes
| Name | Type and value |
|---|---|
USER | 'user' |
PydanticFinalResult
classSource ↗class PydanticFinalResultAttributes
| Name | Type and value |
|---|---|
FINAL_RESULT | 'final_result' |
PydanticAllMessagesEvents
classSource ↗class PydanticAllMessagesEventsAttributes
| Name | Type and value |
|---|---|
ALL_MESSAGES_EVENTS | 'all_messages_events' |
PydanticAllMessages
classSource ↗class PydanticAllMessagesAttributes
| Name | Type and value |
|---|---|
ALL_MESSAGES | 'pydantic_ai.all_messages' |
get_attributes
functionSource ↗def get_attributes(
gen_ai_attrs: Mapping[str, Any],
) -> Iterator[Tuple[str, Any]]Main function to extract OpenInference attributes from GenAI attributes.
Parameters
| Name | Type | Description |
|---|---|---|
gen_ai_attrs | Mapping[str, Any] | Dictionary with OTEL GenAI semantic convention attributes |
Returns
| Type | Description |
|---|---|
Iterator[Tuple[str, Any]] | Iterator of (key, value) pairs for OpenInference attributes |