File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -69,9 +69,12 @@ def set_api_key(self, api_key: str):
69
69
api_key: The OpenAI API key to use. This is the same key used by the OpenAI Python
70
70
client.
71
71
"""
72
- # We're specifically setting the underlying cached property as well
72
+ # Clear the cached property if it exists
73
+ if 'api_key' in self .__dict__ :
74
+ del self .__dict__ ['api_key' ]
75
+
76
+ # Update the private attribute
73
77
self ._api_key = api_key
74
- self .api_key = api_key
75
78
76
79
@cached_property
77
80
def api_key (self ):
Original file line number Diff line number Diff line change
1
+ import os
2
+
3
+ from agents .tracing .processors import BackendSpanExporter
4
+
5
+
6
+ def test_set_api_key_preserves_env_fallback ():
7
+ """Test that set_api_key doesn't break environment variable fallback."""
8
+ # Set up environment
9
+ original_key = os .environ .get ("OPENAI_API_KEY" )
10
+ os .environ ["OPENAI_API_KEY" ] = "env-key"
11
+
12
+ try :
13
+ exporter = BackendSpanExporter ()
14
+
15
+ # Initially should use env var
16
+ assert exporter .api_key == "env-key"
17
+
18
+ # Set explicit key
19
+ exporter .set_api_key ("explicit-key" )
20
+ assert exporter .api_key == "explicit-key"
21
+
22
+ # Clear explicit key and verify env fallback works
23
+ exporter ._api_key = None
24
+ if "api_key" in exporter .__dict__ :
25
+ del exporter .__dict__ ["api_key" ]
26
+ assert exporter .api_key == "env-key"
27
+
28
+ finally :
29
+ if original_key is None :
30
+ os .environ .pop ("OPENAI_API_KEY" , None )
31
+ else :
32
+ os .environ ["OPENAI_API_KEY" ] = original_key
You can’t perform that action at this time.
0 commit comments