{"id":10010,"library":"openinference-instrumentation-pydantic-ai","title":"OpenInference PydanticAI Instrumentation","description":"The `openinference-instrumentation-pydantic-ai` library provides OpenTelemetry instrumentation for the `pydantic-ai` library. It automatically captures and exports telemetry data (traces, spans) for AI model calls made using `pydantic-ai` models, allowing for observability into AI application workflows. The current version is 0.1.12, and it is part of the larger OpenInference project which has a frequent release cadence, often releasing updates across its various instrumentation packages.","status":"active","version":"0.1.12","language":"python","source_language":"en","source_url":"https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-pydantic-ai","tags":["observability","ai","pydantic","instrumentation","opentelemetry","llm","tracing"],"install":[{"cmd":"pip install openinference-instrumentation-pydantic-ai","lang":"bash","label":"Install package"},{"cmd":"pip install pydantic-ai openai opentelemetry-sdk opentelemetry-exporter-otlp","lang":"bash","label":"Install required dependencies for quickstart"}],"dependencies":[{"reason":"Core OpenInference instrumentation library.","package":"openinference-instrumentation","optional":false},{"reason":"The library being instrumented (version >=0.0.17,<0.0.21).","package":"pydantic-ai","optional":false},{"reason":"Required for OpenTelemetry core functionalities.","package":"opentelemetry-sdk","optional":false},{"reason":"Provides standard semantic conventions for telemetry data.","package":"opentelemetry-semantic-conventions","optional":false},{"reason":"Common LLM provider for pydantic-ai models; needed for quickstart example.","package":"openai","optional":true}],"imports":[{"wrong":"from openinference.instrumentation.pydantic_ai import PydanticAIInstrumentor","symbol":"PydanticAIInstrumentor","correct":"from openinference.instrumentation.pydantic_ai import PydanticAIInstrumentor"}],"quickstart":{"code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter\nfrom openinference.instrumentation.pydantic_ai import PydanticAIInstrumentor\n\n# Configure OpenTelemetry to print traces to the console\nresource = Resource.create({\"service.name\": \"my-pydantic-ai-app\"})\ntracer_provider = TracerProvider(resource=resource)\nspan_processor = SimpleSpanProcessor(ConsoleSpanExporter())\ntracer_provider.add_span_processor(span_processor)\ntrace.set_tracer_provider(tracer_provider)\n\n# Instrument pydantic-ai\nPydanticAIInstrumentor().instrument()\n\n# Ensure an OpenAI API key is available for pydantic-ai to function\nos.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\", \"sk-YOUR_OPENAI_KEY_HERE\")\n\n# Use pydantic-ai\nfrom pydantic import BaseModel\nfrom pydantic_ai import AIModel\n\nclass Recipe(AIModel):\n    ingredients: list[str]\n    instructions: list[str]\n    prep_time_minutes: int\n\ndef main():\n    print(\"\\n--- Generating a recipe with PydanticAI ---\\n\")\n    with trace.get_tracer(__name__).start_as_current_span(\"generate_recipe\"):\n        try:\n            recipe = Recipe.generate(\"A simple pasta dish for two.\")\n            print(f\"Ingredients: {recipe.ingredients}\")\n            print(f\"Instructions: {recipe.instructions}\")\n            print(f\"Prep time: {recipe.prep_time_minutes} minutes\\n\")\n        except Exception as e:\n            print(f\"Error generating recipe: {e}\")\n            print(\"Please ensure OPENAI_API_KEY is set and valid, and required dependencies are installed.\")\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"This quickstart demonstrates how to instrument `pydantic-ai` calls using `OpenInferencePydanticAIInstrumentor`. It sets up a console exporter for OpenTelemetry, instruments `pydantic-ai`, and then uses a simple `AIModel` to generate a recipe. Ensure `OPENAI_API_KEY` is set in your environment or replace the placeholder. You'll need `pydantic-ai`, `openai`, `opentelemetry-sdk`, and `opentelemetry-exporter-otlp` installed for this to run."},"warnings":[{"fix":"Initialize `TracerProvider`, `SpanProcessor`, and an `Exporter` (e.g., `OTLPSpanExporter` or `ConsoleSpanExporter`) and set it as the global tracer provider via `trace.set_tracer_provider()` early in your application's lifecycle, before any `pydantic-ai` calls.","message":"OpenTelemetry must be configured (TracerProvider, SpanProcessor, Exporter) before `PydanticAIInstrumentor().instrument()` is called to ensure traces are captured and exported correctly. Failure to do so will result in no telemetry data being emitted.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always check the `install_requires` or `project.dependencies` in the `pyproject.toml` or `setup.py` of `openinference-instrumentation-pydantic-ai` for the supported `pydantic-ai` version range. Ensure your `pydantic-ai` installation adheres to this range. Upgrade both instrumentation and `pydantic-ai` together if newer versions are available.","message":"The `pydantic-ai` library itself is in early development and can have breaking changes, as indicated by its minor version increments. `openinference-instrumentation-pydantic-ai` explicitly pins `pydantic-ai` to a narrow version range (`>=0.0.17,<0.0.21`). Upgrading `pydantic-ai` outside this range without upgrading the instrumentation package may lead to unexpected behavior or `AttributeError`.","severity":"breaking","affected_versions":"All versions up to 0.1.12"},{"fix":"Review the OpenInference documentation and source code for the exact methods being patched. For custom scenarios, consider adding manual OpenTelemetry spans around your `pydantic-ai` calls or contributing to the OpenInference project to extend coverage.","message":"Instrumentation might not capture all details if the underlying `pydantic-ai` model uses custom or unsupported LLM providers or complex callbacks that are not directly patched by the instrumentor. The instrumentation primarily targets the core `pydantic-ai` generation methods.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0.1.12':50 'across':69 'ai':9,18,30,37,43,75 'allow':39 'applic':44 'automat':21 'cadenc':65 'call':32 'captur':22 'current':47 'data':26 'export':24 'frequent':63 'instrument':3,7,13,72,77 'larger':57 'librari':10,19 'llm':79 'made':33 'model':31,38 'observ':41,74 'often':66 'openinfer':1,6,58 'openinference-instrumentation-pydantic-ai':5 'opentelemetri':12,78 'packag':73 'part':54 'project':59 'provid':11 'pydant':8,17,36,76 'pydantic-ai':16,35 'pydanticai':2 'releas':64,67 'span':28 'telemetri':25 'trace':27,80 'updat':68 'use':34 'various':71 'version':48 'workflow':45","created_at":"2026-04-17T01:21:55.324758+00:00","updated_at":"2026-04-17T01:21:55.324758+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\nImportError: cannot import name 'PydanticAIInstrumentor' from 'openinference.instrumentation.pydantic_ai' (/tmp/tmpex82xu_a/venv/lib/python3.12/site-packages/openinference/instrumentation/pydantic_ai/__init__.py)"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.1.16","cli_name":"","cli_version":null,"type":"library","homepage":"https://openinference.com","github":"https://github.com/Arize-ai/openinference","docs":null,"changelog":null,"pypi":"https://pypi.org/project/openinference-instrumentation-pydantic-ai/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["observability","llm-agents","ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"import_fail","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-07-10","install_tag":null}}