{"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":"en","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":[{"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,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install `pydantic-ai` using `pip install pydantic-ai`.","cause":"The `pydantic-ai` library, which this instrumentation targets, is not installed in your environment.","error":"ModuleNotFoundError: No module named 'pydantic_ai'"},{"fix":"Install the instrumentation package using `pip install openinference-instrumentation-pydantic-ai`.","cause":"The `openinference-instrumentation-pydantic-ai` library itself is not installed.","error":"ModuleNotFoundError: No module named 'openinference.instrumentation.pydantic_ai'"},{"fix":"Check the `pydantic-ai` package's dependencies for its compatible `pydantic` versions and ensure your environment matches. You might need to reinstall `pydantic-ai` or `pydantic` with specific versions, e.g., `pip install 'pydantic<2.0.0'` if `pydantic-ai` is not Pydantic V2 compatible.","cause":"This error can occur if you have a version conflict between `pydantic` and `pydantic-ai`, or if `pydantic-ai` is installed with an unsupported `pydantic` version. `pydantic-ai` often has strict Pydantic version requirements.","error":"TypeError: 'ABCMeta' object is not subscriptable"},{"fix":"Verify that `trace.set_tracer_provider()` is called with a configured `TracerProvider` and `SpanProcessor` early in your application's startup. Ensure `PydanticAIInstrumentor().instrument()` is called directly after OpenTelemetry setup.","cause":"OpenTelemetry is either not correctly configured, or the `instrument()` method was called too late. The `TracerProvider` and `SpanProcessor` must be set up before `PydanticAIInstrumentor().instrument()` and before any `pydantic-ai` calls are made.","error":"No traces appear in the console or exporter logs."}]}