{"id":6019,"library":"openinference-instrumentation-openai-agents","title":"OpenInference OpenAI Agents Instrumentation","description":"This library provides OpenTelemetry instrumentation for OpenAI Agents (Assistants API). It automatically captures trace data for interactions with OpenAI's `client.beta.assistants`, `client.beta.threads`, `client.beta.threads.messages`, and `client.beta.threads.runs`, converting them into OpenInference-compliant spans. The current version is 1.4.1, and it's part of the broader OpenInference monorepo, which releases frequently, often with per-library updates.","status":"active","version":"1.4.1","language":"en","source_language":"en","source_url":"https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai-agents","tags":["OpenTelemetry","OpenInference","OpenAI","LLM","observability","tracing","agents","AI","Assistants API"],"install":[{"cmd":"pip install openinference-instrumentation-openai-agents openai opentelemetry-sdk","lang":"bash","label":"Install library and core dependencies"}],"dependencies":[{"reason":"Core OpenInference instrumentation utilities.","package":"openinference-instrumentation","optional":false},{"reason":"The OpenAI Python client library, specifically for the Assistants API (OpenAI Agents). Requires >=1.0.0.","package":"openai","optional":false},{"reason":"The OpenTelemetry SDK is required for trace context propagation and exporting spans.","package":"opentelemetry-sdk","optional":false}],"imports":[{"symbol":"OpenAIInstrumentor","correct":"from openinference.instrumentation.openai_agents import OpenAIInstrumentor"}],"quickstart":{"code":"import os\nimport time\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.openai_agents import OpenAIInstrumentor\nimport openai\n\n# 1. Setup OpenTelemetry (before instrumentation)\nresource = Resource.create({\"service.name\": \"openai-agents-quickstart\"})\ntracer_provider = TracerProvider(resource=resource)\ntracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))\ntrace.set_tracer_provider(tracer_provider)\n\n# 2. Instrument OpenAI Agents\nOpenAIInstrumentor().instrument()\n\n# 3. Initialize OpenAI Client\n# Ensure OPENAI_API_KEY is set in your environment\nclient = openai.OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"\"))\n\ntry:\n    # 4. Interact with OpenAI Assistants API\n    assistant = client.beta.assistants.create(\n        name=\"Math Tutor\",\n        instructions=\"You are a personal math tutor. Answer questions briefly.\",\n        model=\"gpt-4o\",\n    )\n    print(f\"Created Assistant: {assistant.id}\")\n\n    thread = client.beta.threads.create()\n    print(f\"Created Thread: {thread.id}\")\n\n    message = client.beta.threads.messages.create(\n        thread_id=thread.id,\n        role=\"user\",\n        content=\"What is 1 + 1?\",\n    )\n    print(f\"Added Message: {message.id}\")\n\n    run = client.beta.threads.runs.create(\n        thread_id=thread.id,\n        assistant_id=assistant.id,\n        instructions=\"Please address the user as Professor.\"\n    )\n    print(f\"Started Run: {run.id}\")\n\n    # 5. Wait for the run to complete (simulated polling)\n    while run.status in ['queued', 'in_progress', 'cancelling']:\n        time.sleep(1)\n        run = client.beta.threads.runs.retrieve(\n            thread_id=thread.id,\n            run_id=run.id\n        )\n        print(f\"Run status: {run.status}\")\n\n    # 6. Retrieve messages after the run\n    messages = client.beta.threads.messages.list(\n        thread_id=thread.id\n    )\n    print(\"\\nConversation:\")\n    for m in reversed(messages.data):\n        print(f\"{m.role}: {m.content[0].text.value}\")\n\nexcept openai.APIAssistantError as e:\n    print(f\"OpenAI API Error: {e}\")\n    print(\"Please ensure you have an OpenAI API key set and have access to the Assistants API.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\nfinally:\n    # Clean up resources\n    try:\n        if 'thread' in locals() and thread.id:\n            client.beta.threads.delete(thread.id)\n            print(f\"Deleted Thread: {thread.id}\")\n        if 'assistant' in locals() and assistant.id:\n            client.beta.assistants.delete(assistant.id)\n            print(f\"Deleted Assistant: {assistant.id}\")\n    except Exception as e:\n        print(f\"Error during cleanup: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to instrument an OpenAI Assistants API workflow. It sets up an OpenTelemetry TracerProvider, instruments the OpenAI library, creates an assistant, thread, message, and runs the assistant, capturing traces for each step. Remember to set your `OPENAI_API_KEY` environment variable."},"warnings":[{"fix":"Upgrade your `openai` library to version `1.0.0` or higher (`pip install 'openai>=1.0.0,<2'`).","message":"This instrumentation is designed for `openai` library versions 1.0.0 and above. Using `openai` versions 0.x.x will lead to `AttributeError` or incorrect instrumentation as the API structure changed significantly.","severity":"breaking","affected_versions":"<1.0.0 (openai library)"},{"fix":"Always ensure your OpenTelemetry SDK setup (e.g., `trace.set_tracer_provider(...)`) is executed prior to calling `OpenAIInstrumentor().instrument()`.","message":"The OpenTelemetry `TracerProvider` must be configured and set *before* calling `OpenAIInstrumentor().instrument()`. If the order is reversed, the instrumentation might not pick up the `TracerProvider`, resulting in no traces being emitted.","severity":"gotcha","affected_versions":"All"},{"fix":"To prevent sensitive data from being exported, configure `OpenAIInstrumentor` with `hide_inputs=True` and/or `hide_outputs=True` when initializing, e.g., `OpenAIInstrumentor(hide_inputs=True, hide_outputs=True).instrument()`.","message":"By default, the instrumentation captures full inputs and outputs, which may include sensitive or personally identifiable information (PII). Be mindful of data privacy requirements.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}