Skip to main content

OpenInference PydanticAI

pypi

Python auto-instrumentation library for PydanticAI. These traces are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as Arize Phoenix or Arize AX.

Installation

pip install openinference-instrumentation-pydantic-ai

Quickstart

This quickstart shows you how to instrument your PydanticAI agents.

Install required packages.

pip install pydantic-ai arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp

Start Phoenix in the background as a collector. By default, it listens on http://localhost:6006. You can visit the app via a browser at the same address. (Phoenix does not send data over the internet. It only operates locally on your machine.)

phoenix serve

Here's a simple example that demonstrates how to use PydanticAI with OpenInference instrumentation:

import os
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.instrumented import InstrumentationSettings
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation.pydantic_ai import OpenInferenceSpanProcessor
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

# Set up the tracer provider
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)

# Add the OpenInference span processor
endpoint = "http://127.0.0.1:6006/v1/traces"
exporter = OTLPSpanExporter(endpoint=endpoint)
tracer_provider.add_span_processor(OpenInferenceSpanProcessor())
tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))


# Define your Pydantic model
class LocationModel(BaseModel):
    city: str
    country: str

instrumentation = InstrumentationSettings(version=2)

# Create and configure the agent
model = OpenAIModel("gpt-4", provider=OpenAIProvider())
agent = Agent(model, output_type=LocationModel, instrument=instrumentation)

# Run the agent
result = agent.run_sync("The windy city in the US of A.")
print(result)

This example:

  1. Sets up OpenTelemetry tracing with Phoenix
  2. Defines a simple Pydantic model for location data
  3. Creates a PydanticAI agent with instrumentation enabled
  4. Runs a query and gets structured output

The traces will be visible in the Phoenix UI at http://localhost:6006.

More Info

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file openinference_instrumentation_pydantic_ai-0.1.19.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_pydantic_ai-0.1.19.tar.gz
Algorithm Hash digest
SHA256 8e090d92080b57ae1860c99791d8fbbcd88bb445a9664a4dd295a87a3d6dc7ec
MD5 af59ce14e45ddf33a726f167c38df3b0
BLAKE2b-256 c7afe495307b5b85e8abb2aa95fe058af338345eb4f2c8bd419b222f9c648a87

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_pydantic_ai-0.1.19.tar.gz:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openinference_instrumentation_pydantic_ai-0.1.19-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_pydantic_ai-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 74f4e7bb919ae577d70ef140cd00810609b6d28cc4c8d4069b24673cb33b44b5
MD5 93d8668430b271f2d9d6c9e1c60fd0e9
BLAKE2b-256 f968990749fab50c1bfa0ff3057e9fb4e224cf3d795c4d8dcfc93dae84606c4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_pydantic_ai-0.1.19-py3-none-any.whl:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page