Skip to content

Commit dc32378

Browse files
committed
docs(openai-agents): add content-capture and multi-agent samples
1 parent a588bc8 commit dc32378

File tree

8 files changed

+263
-0
lines changed

8 files changed

+263
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Update this with your real OpenAI API key
2+
OPENAI_API_KEY=sk-YOUR_API_KEY
3+
4+
# Uncomment and adjust if you use a non-default OTLP collector endpoint
5+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
6+
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
7+
8+
OTEL_SERVICE_NAME=opentelemetry-python-openai-agents-content-capture
9+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=event_only
10+
11+
# Set to ``no_content`` to opt out of capturing prompts and completions
12+
# OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=no_content
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
OpenTelemetry OpenAI Agents Content Capture Example
2+
===================================================
3+
4+
This example highlights how to customize OpenAI Agents instrumentation to
5+
capture prompts, responses, and tool payloads. By setting
6+
``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=event_only`` the sample
7+
records message content as span events without duplicating it on span
8+
attributes. Use ``span_and_event`` (default) or ``span_only`` for alternative
9+
capture modes, or ``no_content`` to disable capture entirely. Boolean strings
10+
such as ``true``/``false`` are also accepted.
11+
12+
Setup
13+
-----
14+
15+
1. Copy `.env.example <.env.example>`_ to `.env` and update ``OPENAI_API_KEY``.
16+
Adjust the OTLP endpoint variables if you export somewhere other than
17+
``http://localhost:4317``.
18+
2. Create a virtual environment and install the dependencies:
19+
20+
::
21+
22+
python3 -m venv .venv
23+
source .venv/bin/activate
24+
pip install "python-dotenv[cli]"
25+
pip install -r requirements.txt
26+
27+
Run
28+
---
29+
30+
Execute the sample and inspect the exported telemetry to see prompts, tool
31+
arguments, and responses attached as span events:
32+
33+
::
34+
35+
dotenv run -- python main.py
36+
37+
Set the ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`` environment
38+
variable to the capture mode you want (``span_and_event`` by default, or
39+
``span_only``/``event_only``/``no_content``) before running the sample.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# pylint: skip-file
2+
"""Content capture OpenAI Agents instrumentation example."""
3+
4+
from __future__ import annotations
5+
6+
from agents import Agent, Runner, function_tool
7+
from dotenv import load_dotenv
8+
9+
from opentelemetry import trace
10+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
11+
OTLPSpanExporter,
12+
)
13+
from opentelemetry.instrumentation.openai_agents import (
14+
OpenAIAgentsInstrumentor,
15+
)
16+
from opentelemetry.sdk.trace import TracerProvider
17+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
18+
19+
20+
def configure_tracing() -> None:
21+
"""Configure the OpenTelemetry SDK and enable event-only capture."""
22+
23+
provider = TracerProvider()
24+
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
25+
trace.set_tracer_provider(provider)
26+
27+
OpenAIAgentsInstrumentor().instrument(tracer_provider=provider)
28+
29+
30+
@function_tool
31+
def lookup_weather(city: str) -> str:
32+
"""Return a canned weather response for the requested city."""
33+
34+
return f"The weather in {city} is mild with occasional sunshine."
35+
36+
37+
def run_agent() -> None:
38+
"""Create a story-driven packing advisor and execute one run."""
39+
40+
storyteller = Agent(
41+
name="Packing Coach",
42+
instructions=(
43+
"Guide the traveler with tailored packing advice. Use the weather"
44+
" tool for localized details and describe why each item matters."
45+
),
46+
tools=[lookup_weather],
47+
)
48+
49+
result = Runner.run_sync(
50+
storyteller,
51+
"I'm headed to Tokyo in the spring. Help me decide what to pack.",
52+
)
53+
54+
print("Agent response:")
55+
print(result.final_output)
56+
57+
58+
def main() -> None:
59+
load_dotenv()
60+
configure_tracing()
61+
run_agent()
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openai-agents~=0.3.3
2+
python-dotenv~=1.0
3+
4+
opentelemetry-sdk~=1.36.0
5+
opentelemetry-exporter-otlp-proto-grpc~=1.36.0
6+
opentelemetry-instrumentation-openai-agents~=0.1.0.dev
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Update this with your real OpenAI API key
2+
OPENAI_API_KEY=sk-YOUR_API_KEY
3+
4+
# Uncomment and adjust if you use a non-default OTLP collector endpoint
5+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
6+
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
7+
8+
OTEL_SERVICE_NAME=opentelemetry-python-openai-agents-multi-agent
9+
10+
# Set to ``no_content`` to opt out of capturing prompts and completions
11+
# OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=no_content
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
OpenTelemetry OpenAI Agents Multi-Agent Example
2+
===============================================
3+
4+
This example coordinates two specialized agents to produce a combined travel
5+
recommendation. The example captures prompts, responses, and tool interactions
6+
for both agents so you can explore multi-span traces in your GenAI telemetry
7+
backend.
8+
9+
Setup
10+
-----
11+
12+
1. Copy `.env.example <.env.example>`_ to `.env` and update ``OPENAI_API_KEY``.
13+
Adjust the OTLP endpoint variables as needed for your collector.
14+
2. Create a virtual environment and install the dependencies:
15+
16+
::
17+
18+
python3 -m venv .venv
19+
source .venv/bin/activate
20+
pip install "python-dotenv[cli]"
21+
pip install -r requirements.txt
22+
23+
Run
24+
---
25+
26+
Launch both agents and examine the spans they generate:
27+
28+
::
29+
30+
dotenv run -- python main.py
31+
32+
Set ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`` to ``span_only`` or
33+
``event_only`` to limit capture, or to ``no_content`` to suppress prompts and
34+
responses entirely for this scenario. Leaving it unset records content in both
35+
span attributes and events. Boolean strings such as ``true``/``false`` are also
36+
accepted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# pylint: skip-file
2+
"""Multi-agent OpenAI Agents instrumentation example."""
3+
4+
from __future__ import annotations
5+
6+
from agents import Agent, Runner, function_tool
7+
from dotenv import load_dotenv
8+
9+
from opentelemetry import trace
10+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
11+
OTLPSpanExporter,
12+
)
13+
from opentelemetry.instrumentation.openai_agents import (
14+
OpenAIAgentsInstrumentor,
15+
)
16+
from opentelemetry.sdk.trace import TracerProvider
17+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
18+
19+
20+
def configure_tracing() -> None:
21+
"""Configure tracing with the default content capture settings."""
22+
23+
provider = TracerProvider()
24+
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
25+
trace.set_tracer_provider(provider)
26+
27+
OpenAIAgentsInstrumentor().instrument(tracer_provider=provider)
28+
29+
30+
@function_tool
31+
def lookup_weather(city: str) -> str:
32+
"""Return a canned weekend forecast for the requested city."""
33+
34+
return f"Expect pleasant spring weather in {city} with cool evenings."
35+
36+
37+
@function_tool
38+
def find_dining(city: str) -> str:
39+
"""Return a canned dining suggestion for the requested city."""
40+
41+
return (
42+
f"Reserve a table at a neighborhood izakaya in {city} for fresh seafood"
43+
" and seasonal specials."
44+
)
45+
46+
47+
def run_team() -> None:
48+
"""Coordinate two agents and show their combined guidance."""
49+
50+
travel_planner = Agent(
51+
name="Travel Planner",
52+
instructions=(
53+
"Design a short itinerary with highlights and a quick travel tip."
54+
),
55+
tools=[lookup_weather],
56+
)
57+
58+
dining_expert = Agent(
59+
name="Dining Expert",
60+
instructions=(
61+
"Recommend a memorable dining experience that complements the"
62+
" travel plan provided."
63+
),
64+
tools=[find_dining],
65+
)
66+
67+
traveler_question = "I'm visiting Kyoto for a long weekend. Any advice?"
68+
69+
itinerary = Runner.run_sync(travel_planner, traveler_question)
70+
dining = Runner.run_sync(
71+
dining_expert,
72+
f"Given this plan: {itinerary.final_output}\nSuggest food options.",
73+
)
74+
75+
print("Travel planner response:")
76+
print(itinerary.final_output)
77+
print("\nDining expert response:")
78+
print(dining.final_output)
79+
80+
81+
def main() -> None:
82+
load_dotenv()
83+
configure_tracing()
84+
run_team()
85+
86+
87+
if __name__ == "__main__":
88+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openai-agents~=0.3.3
2+
python-dotenv~=1.0
3+
4+
opentelemetry-sdk~=1.36.0
5+
opentelemetry-exporter-otlp-proto-grpc~=1.36.0
6+
opentelemetry-instrumentation-openai-agents~=0.1.0.dev

0 commit comments

Comments
 (0)