Python APIpydantic-ai

Core

Instrumentation

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-ai

openinference.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

NameTypeDefaultDescription
span_filterOptional[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,
) -> None

Called when a span starts. No processing needed at start.

on_end

methodSource ↗
def on_end(span: ReadableSpan) -> None

Process a span when it ends by enhancing it with OpenInference attributes.

shutdown

methodSource ↗
def shutdown() -> None

Shutdown the processor. No cleanup needed.

force_flush

methodSource ↗
def force_flush(timeout_millis: int = 30000) -> bool

Force flush any pending operations. Always returns True as there’s nothing to flush.

is_openinference_span

functionSource ↗
def is_openinference_span(span: ReadableSpan) -> bool

Also 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,
) -> bool

Determine if a span should be exported based on a filter.

openinference.instrumentation.pydantic_ai.semantic_conventions

OTELConventions

classSource ↗
class OTELConventions

Attributes

NameType and value
EVENTS'events'
EVENT_NAME'event.name'

GenAIConversationID

classSource ↗
class GenAIConversationID

Attributes

NameType and value
CONVERSATION_ID'gen_ai.conversation.id'

GenAIEventNames

classSource ↗
class GenAIEventNames

Attributes

NameType 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 GenAIMessageFields

Attributes

NameType and value
ID'id'
CONTENT'content'
ROLE'role'
PARTS'parts'
TOOL_CALLS'tool_calls'

GenAIMessageRoles

classSource ↗
class GenAIMessageRoles

Attributes

NameType and value
USER'user'
ASSISTANT'assistant'
SYSTEM'system'
TOOL'tool'

GenAIMessagePartFields

classSource ↗
class GenAIMessagePartFields

Attributes

NameType and value
TYPE'type'
CONTENT'content'
RESULT'result'
NAME'name'

GenAIMessagePartTypes

classSource ↗
class GenAIMessagePartTypes

Attributes

NameType and value
TEXT'text'
TOOL_CALL'tool_call'
TOOL_CALL_RESPONSE'tool_call_response'

GenAIToolCallFields

classSource ↗
class GenAIToolCallFields

Attributes

NameType and value
FUNCTION'function'
ID'id'
TYPE'type'

GenAIFunctionFields

classSource ↗
class GenAIFunctionFields

Attributes

NameType and value
NAME'name'
ARGUMENTS'arguments'

GenAIChoiceBodyFields

classSource ↗
class GenAIChoiceBodyFields

Attributes

NameType and value
INDEX'index'
FINISH_REASON'finish_reason'
MESSAGE'message'
TOOL_CALLS'tool_calls'

GenAISystemInstructionsFields

classSource ↗
class GenAISystemInstructionsFields

Attributes

NameType and value
TYPE'type'
CONTENT'content'

PydanticAgentName

classSource ↗
class PydanticAgentName

Attributes

NameType and value
AGENT'agent_name'

PydanticTools

classSource ↗
class PydanticTools

Attributes

NameType and value
TOOLS'tools'
TOOL_ARGUMENTS'tool_arguments'
TOOL_RESPONSE'tool_response'

PydanticGenAIAttribute

classSource ↗
class PydanticGenAIAttribute

Attributes

NameType and value
GEN_AI'gen_ai'

PydanticGenAITool

classSource ↗
class PydanticGenAITool

Attributes

NameType and value
TOOL'tool'

PydanticCustomAttributes

classSource ↗
class PydanticCustomAttributes

Attributes

NameType and value
MODEL_REQUEST_PARAMETERS'model_request_parameters'

PydanticModelRequestParameters

classSource ↗
class PydanticModelRequestParameters

Attributes

NameType and value
TOOLS'output_tools'
FUNCTION_TOOLS'function_tools'
NAME'name'
DESCRIPTION'description'
PARAMETERS'parameters'

PydanticModelRequestParametersTool

classSource ↗
class PydanticModelRequestParametersTool

Attributes

NameType and value
NAME'name'
DESCRIPTION'description'
PARAMETERS_JSON_SCHEMA'parameters_json_schema'
PARAMETERS'properties'

PydanticMessageRoleUser

classSource ↗
class PydanticMessageRoleUser

Attributes

NameType and value
USER'user'

PydanticFinalResult

classSource ↗
class PydanticFinalResult

Attributes

NameType and value
FINAL_RESULT'final_result'

PydanticAllMessagesEvents

classSource ↗
class PydanticAllMessagesEvents

Attributes

NameType and value
ALL_MESSAGES_EVENTS'all_messages_events'

PydanticAllMessages

classSource ↗
class PydanticAllMessages

Attributes

NameType 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

NameTypeDescription
gen_ai_attrsMapping[str, Any]

Dictionary with OTEL GenAI semantic convention attributes

Returns

TypeDescription
Iterator[Tuple[str, Any]]

Iterator of (key, value) pairs for OpenInference attributes