From ce488aa261f1d956948c720474074ed4e6070b6d Mon Sep 17 00:00:00 2001 From: Daniel Hashmi <151331476+DanielHashmi@users.noreply.github.com> Date: Fri, 1 Aug 2025 13:22:27 +0500 Subject: [PATCH 1/4] Update processors.py --- src/agents/tracing/processors.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/agents/tracing/processors.py b/src/agents/tracing/processors.py index 54858175e..32fd290ec 100644 --- a/src/agents/tracing/processors.py +++ b/src/agents/tracing/processors.py @@ -69,9 +69,12 @@ def set_api_key(self, api_key: str): api_key: The OpenAI API key to use. This is the same key used by the OpenAI Python client. """ - # We're specifically setting the underlying cached property as well + # Clear the cached property if it exists + if 'api_key' in self.__dict__: + del self.__dict__['api_key'] + + # Update the private attribute self._api_key = api_key - self.api_key = api_key @cached_property def api_key(self): From 3cd5c0aa6b15ec78f7e1597f526ab7dc9406937f Mon Sep 17 00:00:00 2001 From: Daniel Hashmi <151331476+DanielHashmi@users.noreply.github.com> Date: Fri, 1 Aug 2025 13:26:21 +0500 Subject: [PATCH 2/4] Create test_set_api_key_fix.py --- tests/tracing/test_set_api_key_fix.py | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/tracing/test_set_api_key_fix.py diff --git a/tests/tracing/test_set_api_key_fix.py b/tests/tracing/test_set_api_key_fix.py new file mode 100644 index 000000000..8022d9fe3 --- /dev/null +++ b/tests/tracing/test_set_api_key_fix.py @@ -0,0 +1,32 @@ +import os + +from agents.tracing.processors import BackendSpanExporter + + +def test_set_api_key_preserves_env_fallback(): + """Test that set_api_key doesn't break environment variable fallback.""" + # Set up environment + original_key = os.environ.get("OPENAI_API_KEY") + os.environ["OPENAI_API_KEY"] = "env-key" + + try: + exporter = BackendSpanExporter() + + # Initially should use env var + assert exporter.api_key == "env-key" + + # Set explicit key + exporter.set_api_key("explicit-key") + assert exporter.api_key == "explicit-key" + + # Clear explicit key and verify env fallback works + exporter._api_key = None + if "api_key" in exporter.__dict__: + del exporter.__dict__["api_key"] + assert exporter.api_key == "env-key" + + finally: + if original_key is None: + os.environ.pop("OPENAI_API_KEY", None) + else: + os.environ["OPENAI_API_KEY"] = original_key From b143c612ce5861b50b49b3809eeea248b382068b Mon Sep 17 00:00:00 2001 From: Daniel Hashmi <151331476+DanielHashmi@users.noreply.github.com> Date: Fri, 1 Aug 2025 13:26:53 +0500 Subject: [PATCH 3/4] Update tests.yml --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index edd0d898b..810bc58e7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - fix-set-api-key-cached-property pull_request: # All PRs, including stacked PRs From 1e0837546a7a8e577ef4565c9933ccfe0a48535d Mon Sep 17 00:00:00 2001 From: Daniel Hashmi <151331476+DanielHashmi@users.noreply.github.com> Date: Fri, 1 Aug 2025 13:35:01 +0500 Subject: [PATCH 4/4] Run CI on all commits, not just ones on main (#521) --- .github/workflows/tests.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 810bc58e7..edd0d898b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,7 +4,6 @@ on: push: branches: - main - - fix-set-api-key-cached-property pull_request: # All PRs, including stacked PRs