Skip to content

Support OPENAI_BASE_URL in tracing #997

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/agents/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from .spans import Span
from .traces import Trace

TRACES_INGEST_PATH: str = "/traces/ingest"
DEFAULT_TRACES_INGEST_ENDPOINT: str = f"https://api.openai.com/v1{TRACES_INGEST_PATH}"


class ConsoleSpanExporter(TracingExporter):
"""Prints the traces and spans to the console."""
Expand All @@ -33,7 +36,7 @@ def __init__(
api_key: str | None = None,
organization: str | None = None,
project: str | None = None,
endpoint: str = "https://api.openai.com/v1/traces/ingest",
endpoint: str = DEFAULT_TRACES_INGEST_ENDPOINT,
max_retries: int = 3,
base_delay: float = 1.0,
max_delay: float = 30.0,
Expand All @@ -46,15 +49,24 @@ def __init__(
`os.environ["OPENAI_ORG_ID"]` if not provided.
project: The OpenAI project to use. Defaults to
`os.environ["OPENAI_PROJECT_ID"]` if not provided.
endpoint: The HTTP endpoint to which traces/spans are posted.
endpoint: The HTTP endpoint to which traces/spans are posted. If not
overridden and ``OPENAI_BASE_URL`` is set, the endpoint will be
``OPENAI_BASE_URL`` appended with ``/traces/ingest``.
max_retries: Maximum number of retries upon failures.
base_delay: Base delay (in seconds) for the first backoff.
max_delay: Maximum delay (in seconds) for backoff growth.
"""
self._api_key = api_key
self._organization = organization
self._project = project
self.endpoint = endpoint

env_base_url = os.environ.get("OPENAI_BASE_URL")
if env_base_url and endpoint == DEFAULT_TRACES_INGEST_ENDPOINT:
env_base_url = env_base_url.rstrip("/")
self.endpoint = f"{env_base_url}{TRACES_INGEST_PATH}"
else:
self.endpoint = endpoint

self.max_retries = max_retries
self.base_delay = base_delay
self.max_delay = max_delay
Expand Down
14 changes: 14 additions & 0 deletions tests/test_trace_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,17 @@ def test_backend_span_exporter_close(mock_client):

# Ensure underlying http client is closed
mock_client.return_value.close.assert_called_once()


@patch("httpx.Client")
def test_backend_span_exporter_endpoint_from_env(mock_client):
with patch.dict(os.environ, {"OPENAI_BASE_URL": "https://example.com/v1"}):
exporter = BackendSpanExporter(api_key="test_key")
assert exporter.endpoint == "https://example.com/v1/traces/ingest"


@patch("httpx.Client")
def test_backend_span_exporter_custom_endpoint_respected(mock_client):
with patch.dict(os.environ, {"OPENAI_BASE_URL": "https://example.com/v1"}):
exporter = BackendSpanExporter(api_key="test_key", endpoint="https://foo.invalid/trace")
assert exporter.endpoint == "https://foo.invalid/trace"