From 317e849fbdec02ea81618f8783343b10045ed74c Mon Sep 17 00:00:00 2001 From: Corry Haines Date: Wed, 2 Jul 2025 16:39:14 -0700 Subject: [PATCH] Use constants for tracing endpoint --- src/agents/tracing/processors.py | 18 +++++++++++++++--- tests/test_trace_processor.py | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/agents/tracing/processors.py b/src/agents/tracing/processors.py index 73f73312..2b6d0d6f 100644 --- a/src/agents/tracing/processors.py +++ b/src/agents/tracing/processors.py @@ -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.""" @@ -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, @@ -46,7 +49,9 @@ 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. @@ -54,7 +59,14 @@ def __init__( 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 diff --git a/tests/test_trace_processor.py b/tests/test_trace_processor.py index 72318caa..aa6c6091 100644 --- a/tests/test_trace_processor.py +++ b/tests/test_trace_processor.py @@ -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"