OpenAIConversationsSession and OpenAIResponsesCompactionSession ignore set_default_openai_key()
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/agents/memory/openai_conversations_session.py 开始,查看第 21 行和第 50 行附近构建回退 AsyncOpenAI() 的位置,然后检查 src/agents/memory/openai_responses_compaction_session.py 第 196 行附近,以及 src/agents/models/openai_provider.py 中需要复制的 get_default_openai_key() 模式。在两个阶段分别运行 issue 中的复现脚本,然后运行 tests/memory。当两个会话都能报告 set_default_openai_key() 设置的密钥,无论是否设置了 OPENAI_API_KEY,即为完成。
由索引模型根据 Issue 内容生成。
描述
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).
- 主要语言
- Python
- 星标
- 30.5k
- 派生
- 5k
- 平均合并
- 19 小时 30 分钟
- 30 天内合并 PR
- 129
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-agents-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-agents-python#5534 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
openai/openai-agents-python#5529 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
openai/openai-agents-python#5498 ·
维护者通常 1 天内回复
-
Preserve wire aliases when converting Responses output items to input可能已有人在做 @LVsarff 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
openai/openai-agents-python#5477 · 2 条评论 ·
维护者通常 1 天内回复
-
Voice: default sentence splitter never splits Chinese/Japanese, so TTS waits for the whole reply未关闭
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-agents-python#5475 · 1 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-agents-python 的全部 Issue
相似的 Issue
-
documentation
难度 2/5 1-3 小时 新手友好度 76/100
awslabs/social-messaging-agent-runtime-toolkit#140 ·
维护者通常 1 天内回复
-
subtask
难度 2/5 1-3 小时 新手友好度 72/100
fga-eps-mds/2026.2-MeasureSoftGram-DOC#187 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
txn2/mcp-data-platform#2493 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
YO-WHATS-UP2/minigit#78 ·
维护者通常 3 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 78/100
liberation-data/drivine-python#432 ·
维护者通常 1 天内回复