OpenAIConversationsSession and OpenAIResponsesCompactionSession ignore set_default_openai_key()
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
Direzione di ricerca
Inizia da src/agents/memory/openai_conversations_session.py, dove viene costruito il AsyncOpenAI() di fallback intorno alle righe 21 e 50, poi controlla src/agents/memory/openai_responses_compaction_session.py intorno alla riga 196 e src/agents/models/openai_provider.py per il pattern get_default_openai_key() da copiare. Esegui lo script di riproduzione dell'issue in entrambe le fasi, poi tests/memory. È fatto quando entrambe le sessioni riportano la chiave impostata da set_default_openai_key(), che OPENAI_API_KEY sia impostata o meno.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Please read this first
- Have you read the docs? Yes
- Have you searched for related issues? Yes. Nothing covers it. #2164 is a feature request for several keys per run.
Describe the bug
set_default_openai_key() is documented as the key that "will be used instead of the OPENAI_API_KEY environment variable". OpenAIProvider honors it, and so do the voice provider and the Codex tool. OpenAIConversationsSession and OpenAIResponsesCompactionSession don't. When no default client is set, they build a bare AsyncOpenAI(), which reads only OPENAI_API_KEY. That causes two problems:
- When the key is set only through the SDK,
OpenAIConversationsSession()raisesOpenAIError: Missing credentialswhen it's constructed, and the compaction session raises on its firstresponses.compactcall. - When
OPENAI_API_KEYis also set, the sessions quietly use the environment key while model calls use the SDK key. Conversation items and the billedresponses.compactcalls land in a different project or org than the run's responses.
Debug information
- Agents SDK version:
main(e355d69), also 0.24.0 - Related library versions: openai 3.29.0
- Python version: 3.14.7
- Operating system: macOS
- Model and model provider: OpenAI
- Does the issue reproduce with the latest Agents SDK release? Yes
- Does the issue occur consistently or intermittently? Consistently
Repro steps
import os
os.environ.pop("OPENAI_API_KEY", None) # key is configured only through the SDK
from agents import SQLiteSession, set_default_openai_key
from agents.memory import OpenAIConversationsSession, OpenAIResponsesCompactionSession
from agents.models.multi_provider import MultiProvider
set_default_openai_key("sk-from-set-default-openai-key")
def show(label, make):
try:
print(f"{label}:", make().api_key)
except Exception as e:
print(f"{label}:", type(e).__name__, "-", str(e)[:40])
for phase in ("SDK key only", "OPENAI_API_KEY also set"):
if phase == "OPENAI_API_KEY also set":
os.environ["OPENAI_API_KEY"] = "sk-other-project-from-env"
print("--", phase)
print("model client :", MultiProvider().get_model("gpt-4.1")._client.api_key)
show("ConversationsSession", lambda: OpenAIConversationsSession()._openai_client)
show("CompactionSession ", lambda: OpenAIResponsesCompactionSession("s", SQLiteSession("s")).client)
-- SDK key only
model client : sk-from-set-default-openai-key
ConversationsSession: OpenAIError - Missing credentials. Please pass a
CompactionSession : OpenAIError - Missing credentials. Please pass a
-- OPENAI_API_KEY also set
model client : sk-from-set-default-openai-key
ConversationsSession: sk-other-project-from-env
CompactionSession : sk-other-project-from-env
Expected behavior
With no client passed and no default client set, both sessions use the key from set_default_openai_key(), as model calls do.
Root cause and a possible fix
The sessions fall back to AsyncOpenAI(): openai_conversations_session.py#L21, #L50 and openai_responses_compaction_session.py#L196. The provider passes get_default_openai_key() instead (openai_provider.py#L156-L161).
--- a/src/agents/memory/openai_conversations_session.py
+++ b/src/agents/memory/openai_conversations_session.py
-from agents.models._openai_shared import get_default_openai_client
+from agents.models._openai_shared import get_default_openai_client, get_default_openai_key
@@ async def start_openai_conversations_session(
- _maybe_openai_client = default_client if default_client is not None else AsyncOpenAI()
+ _maybe_openai_client = (
+ default_client
+ if default_client is not None
+ else AsyncOpenAI(api_key=get_default_openai_key())
+ )
@@ class OpenAIConversationsSession(SessionABC):
- _openai_client = default_client if default_client is not None else AsyncOpenAI()
+ _openai_client = (
+ default_client
+ if default_client is not None
+ else AsyncOpenAI(api_key=get_default_openai_key())
+ )
--- a/src/agents/memory/openai_responses_compaction_session.py
+++ b/src/agents/memory/openai_responses_compaction_session.py
-from ..models._openai_shared import get_default_openai_client
+from ..models._openai_shared import get_default_openai_client, get_default_openai_key
@@ def client(self) -> AsyncOpenAI:
- self._client = default_client if default_client is not None else AsyncOpenAI()
+ self._client = (
+ default_client
+ if default_client is not None
+ else AsyncOpenAI(api_key=get_default_openai_key())
+ )
AsyncOpenAI(api_key=None) still falls back to OPENAI_API_KEY, so nothing changes for callers who haven't set a default key. With the patch, both sessions report the SDK key in both phases, and tests/memory passes (444 passed).
- Lingua principale
- Python
- Stelle
- 30.5k
- Fork
- 5k
- Merge medio
- 22h 21m
- PR unite (30g)
- 98
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-agents-python
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
openai/openai-agents-python#5498 ·
I maintainer di solito rispondono entro 1 giorno
-
Preserve wire aliases when converting Responses output items to inputForse già presa @LVsarff l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
openai/openai-agents-python#5477 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Voice: default sentence splitter never splits Chinese/Japanese, so TTS waits for the whole replyAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-agents-python#5475 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
tool_choice naming a function tool called web_search, file_search, mcp, etc. selects the built-in tool insteadForse già presa @LVsarff l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
openai/openai-agents-python#5472 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
openai/openai-agents-python#5468 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-agents-python
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
autowarefoundation/autoware_carla_scenario#1485 ·
I maintainer di solito rispondono entro 1 giorno
-
needs-area-label
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 2 giorni
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
Official-Space-AI/OpenPhysGraspGen#114 ·
I maintainer di solito rispondono entro 2 giorni
-
changelog investigate
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
ramnes/notion-sdk-py#441 ·
-
copy_tree splits same-file errors into charactersForse già presa @Gambit-Checkmate l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 14 giorni