{"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install openinference-instrumentation-pydantic-ai","pip install pydantic-ai openai opentelemetry-sdk opentelemetry-exporter-otlp"],"cli":null},"imports":["from openinference.instrumentation.pydantic_ai import PydanticAIInstrumentor"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}