Skip to content

Commit 6168340

Browse files
committed
Read tracing API data lazily
1 parent 090e79b commit 6168340

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/agents/tracing/processors.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import random
66
import threading
77
import time
8+
from functools import cached_property
89
from typing import Any
910

1011
import httpx
@@ -50,9 +51,9 @@ def __init__(
5051
base_delay: Base delay (in seconds) for the first backoff.
5152
max_delay: Maximum delay (in seconds) for backoff growth.
5253
"""
53-
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
54-
self.organization = organization or os.environ.get("OPENAI_ORG_ID")
55-
self.project = project or os.environ.get("OPENAI_PROJECT_ID")
54+
self._api_key = api_key
55+
self._organization = organization
56+
self._project = project
5657
self.endpoint = endpoint
5758
self.max_retries = max_retries
5859
self.base_delay = base_delay
@@ -61,14 +62,30 @@ def __init__(
6162
# Keep a client open for connection pooling across multiple export calls
6263
self._client = httpx.Client(timeout=httpx.Timeout(timeout=60, connect=5.0))
6364

65+
self._lazy_read_complete = False
66+
6467
def set_api_key(self, api_key: str):
6568
"""Set the OpenAI API key for the exporter.
6669
6770
Args:
6871
api_key: The OpenAI API key to use. This is the same key used by the OpenAI Python
6972
client.
7073
"""
71-
self.api_key = api_key
74+
# Reset the cached property
75+
del self.api_key
76+
self._api_key = api_key
77+
78+
@cached_property
79+
def api_key(self):
80+
return self._api_key or os.environ.get("OPENAI_API_KEY")
81+
82+
@cached_property
83+
def organization(self):
84+
return self._organization or os.environ.get("OPENAI_ORG_ID")
85+
86+
@cached_property
87+
def project(self):
88+
return self._project or os.environ.get("OPENAI_PROJECT_ID")
7289

7390
def export(self, items: list[Trace | Span[Any]]) -> None:
7491
if not items:

0 commit comments

Comments
 (0)