-
Notifications
You must be signed in to change notification settings - Fork 13
add OpenAI Agents SDK zero-code example #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
krisztianfekete
merged 6 commits into
agentevals-dev:main
from
shahar-dagan:feat/openai-agents-upstream
Mar 31, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33c4cc3
add OpenAI Agents SDK zero-code example and e2e tests
shahar-dagan 5d0522f
Merge pull request #1 from shahar-dagan/feat/openai-agents-zero-code-…
shahar-dagan eaf7109
add OpenAI Agents SDK zero-code example and e2e tests
shahar-dagan 28a1e91
Merge branch 'main' into feat/openai-agents-upstream
shahar-dagan 0dd669a
address PR review feedback
shahar-dagan a625609
use setdefault for OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT
shahar-dagan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| openai-agents>=0.3.3 | ||
| opentelemetry-instrumentation-openai-agents-v2>=0.1.0 | ||
|
|
||
| opentelemetry-sdk>=1.36.0 | ||
| opentelemetry-exporter-otlp-proto-http>=1.36.0 | ||
| python-dotenv>=1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Run a dice-rolling OpenAI Agents SDK agent with OTLP export — no agentevals SDK. | ||
|
|
||
| Demonstrates zero-code integration: any OTel-instrumented agent streams | ||
| traces to agentevals by pointing the OTLP exporter at the receiver. | ||
|
|
||
| Unlike the LangChain and Strands examples, this one is fully self-contained: | ||
| the agent code lives inline with no cross-folder imports. | ||
|
|
||
| Prerequisites: | ||
| 1. pip install -r requirements.txt | ||
| 2. agentevals serve --dev | ||
| 3. export OPENAI_API_KEY="your-key-here" | ||
|
|
||
| Usage: | ||
| python examples/zero-code-examples/openai-agents/run.py | ||
| """ | ||
|
|
||
| import os | ||
| import random | ||
|
|
||
| from agents import Agent, Runner, function_tool | ||
| from dotenv import load_dotenv | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor | ||
| from opentelemetry.sdk.resources import Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| load_dotenv(override=True) | ||
|
|
||
|
|
||
| @function_tool | ||
| def roll_die(sides: int) -> int: | ||
| """Roll a die with the given number of sides and return the result.""" | ||
| return random.randint(1, sides) | ||
|
|
||
|
|
||
| @function_tool | ||
| def check_prime(number: int) -> bool: | ||
| """Return True if the number is prime, False otherwise.""" | ||
| if number < 2: | ||
| return False | ||
| for i in range(2, int(number**0.5) + 1): | ||
| if number % i == 0: | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def main(): | ||
| if not os.getenv("OPENAI_API_KEY"): | ||
| print("OPENAI_API_KEY not set.") | ||
| return | ||
|
|
||
| endpoint = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318") | ||
| print(f"OTLP endpoint: {endpoint}") | ||
|
|
||
| os.environ.setdefault("OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", "span_and_event") | ||
|
|
||
| os.environ.setdefault( | ||
| "OTEL_RESOURCE_ATTRIBUTES", | ||
| "agentevals.eval_set_id=openai_agents_eval,agentevals.session_name=openai-agents-zero-code", | ||
| ) | ||
|
|
||
| resource = Resource.create() | ||
|
|
||
| tracer_provider = TracerProvider(resource=resource) | ||
| tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(), schedule_delay_millis=1000)) | ||
| trace.set_tracer_provider(tracer_provider) | ||
|
|
||
| OpenAIAgentsInstrumentor().instrument() | ||
|
|
||
| agent = Agent( | ||
| name="Dice Agent", | ||
| instructions="You are a helpful assistant. You can roll dice and check if numbers are prime.", | ||
| tools=[roll_die, check_prime], | ||
| ) | ||
|
|
||
| test_queries = [ | ||
| "Hi! Can you help me?", | ||
| "Roll a 20-sided die for me", | ||
| "Is the number you rolled prime?", | ||
| ] | ||
|
|
||
| conversation_input: list = [] | ||
|
|
||
| try: | ||
| for i, query in enumerate(test_queries, 1): | ||
| print(f"\n[{i}/{len(test_queries)}] User: {query}") | ||
|
|
||
| conversation_input.append({"role": "user", "content": query}) | ||
| result = Runner.run_sync(agent, conversation_input) | ||
|
|
||
| agent_response = result.final_output or "" | ||
| print(f" Agent: {agent_response}") | ||
|
|
||
| conversation_input = result.to_input_list() | ||
| finally: | ||
| print() | ||
| tracer_provider.force_flush() | ||
| print("All traces flushed to OTLP receiver.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.