> ## Documentation Index
> Fetch the complete documentation index at: https://agent-observability-docs.splunk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic AI

> Learn how to integrate a Pydantic AI project with Splunk Agent Observability using OpenTelemetry

Splunk Agent Observability supports logging traces from [Pydantic AI](https://ai.pydantic.dev/) applications using OpenTelemetry.

## Set up OpenTelemetry

To log Pydantic AI applications using Splunk Agent Observability, the first step is to set up OpenTelemetry.

<Steps>
  <Step title="Install the required packages">
    Add the Splunk Agent Observability Python SDK and Pydantic AI to your Python environment.

    <CodeGroup>
      ```bash Terminal theme={null}
      pip install pydantic-ai \
            splunk-ao \
            opentelemetry-api \
            opentelemetry-sdk
      ```
    </CodeGroup>
  </Step>

  <Step title="Create environment variables for your Splunk Agent Observability settings">
    Set environment variables for your Splunk Agent Observability settings, for example in a `.env` file:

    <CodeGroup>
      ```ini .env theme={null}
      # Your Splunk Agent Observability API key
      # Only required if you are using an
      # on-premises, standalone, or custom deployment
      # SPLUNK_AO_API_KEY="your-splunk-ao-api-key"

      # Your Splunk Agent Observability project name
      SPLUNK_AO_PROJECT="your-splunk-ao-project-name"

      # The name of the Agent Stream you want to use for logging
      SPLUNK_AO_AGENT_STREAM="your-splunk-ao-agent-stream"
      ```
    </CodeGroup>
  </Step>

  <Step title="Get your endpoint">
    The OTel endpoint is different from Splunk Agent Observability's regular API endpoint and is specifically designed to receive telemetry data in the OTLP format.

    If you are using:

    * A cloud deployment, then you don't need to provide a custom OTel endpoint.
      The default endpoint `<your-splunk-ao-api-url>/otel/traces` will be used automatically.

    * A **self-hosted deployment**, replace the `<your-splunk-ao-api-url>/otel/traces` endpoint with your deployment URL. The format of this URL is based on your console URL, appending `/otel/traces`.
  </Step>

  <Step title="Configure the OpenTelemetry tracer provider">
    Use the following code to configure OpenTelemetry with Splunk Agent Observability. This sets up the tracer provider and enables instrumentation for all Pydantic AI agents.

    ```python Python theme={null}
    from splunk_ao import otel
    from opentelemetry import trace
    from opentelemetry.sdk.trace import TracerProvider
    from pydantic_ai import Agent

    # Create and configure the tracer provider
    provider = TracerProvider()
    trace.set_tracer_provider(provider)

    # Add the Splunk Agent Observability span processor
    otel.add_splunk_ao_span_processor(
        tracer_provider=provider,
        processor=otel.SplunkAOSpanProcessor()
    )

    # Enable instrumentation on all agents
    Agent.instrument_all()
    ```
  </Step>
</Steps>

## What gets logged

Pydantic AI spans are automatically detected and normalized by Splunk Agent Observability's OpenTelemetry extension. The following span types are supported:

* **Agent runs**: Complete agent executions with input/output messages
* **Chat completions**: LLM calls with messages and responses
* **Tool executions**: Individual tool invocations with arguments and results
* **Tool workflows**: Parent spans that group multiple tool executions

For a complete working example, see the [Splunk Agent Observability SDK examples repository](https://github.com/splunk/splunk-ao-python/tree/main/examples).
