-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Bug]: SDK crashes when choices
is None
(provider-error payload)
#604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@cnm13ryan I’m happy to create a PR if you point me to a contributors doc or let me know what you need from testing perspective. I’ll create PR from my fork. Assign the issue to me if you’d like me to do that. I can also check out other PRs to see what folks are doing. Thanks. |
Not sure if there is a contributor doc as I cannot find it anywhere. I have some ideas on the fixes. Let me polish that and get it back in a min. |
Follow-up: Root-Cause Analysis (RCA) & Some Thoughts on Remediation Options and Success Criteria1 · Context Recap
2 · Some Analysis
3 · Root-Cause Hypotheses
4. Root Cause likely to be
5 · Option Matrix
6 · RecommendationShip Option A immediately to stop crashes, then schedule Option B for robust, schema-tolerant handling. Option A (Guard: provider may return an error object with choices == None)# agents/models/openai_chatcompletions.py
+ if not getattr(response, "choices", None):
+ raise ProviderError(
+ f"LLM provider error: {getattr(response, 'error', 'unknown')}"
) Outcome: callers receive a clear, catchable ProviderError; negative token streaming or logging impact is zero; no public behaviour regressed. |
Basically, to cut down all the jargon, one way this error arises is when the model service provider information are not configured "as is" to OpenAI chat completion schema by the user (i.e., some typos), but instead of raising the error in pointing the developer or user to the right direction, it just outputs the error mentioned which is not useful unless one digs. Option A is just a quick implementation to raise errors. Option B goes further in terms of providing the compat-layer (separation concerns) to avoid these errors to permeate into domain logic. |
@cnm13ryan the repro script above did not work. It attempts to run the completion model as if it were an agent, which is incorrect. I created a new repro script below. This correctly reproduces the bug and validates the fix. import asyncio
import os
from agents import AsyncOpenAI
from agents.model_settings import ModelSettings
from agents.models.interface import ModelTracing
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
async def main():
api_key = os.environ.get("OPENAI_API_KEY")
client = AsyncOpenAI(
api_key=api_key,
base_url="https://openrouter.ai/v1",
)
# Pass the client into the model constructor
model = OpenAIChatCompletionsModel(
model="gpt-3.5-turbo",
openai_client=client,
)
settings = ModelSettings(
temperature=0.7,
max_tokens=100,
)
result = await model.get_response(
system_instructions="This is a bug repro test. You are a helpful assistant.",
input="Ping?",
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
return result
if __name__ == "__main__":
result = asyncio.run(main())
print(result) |
I created a PR with your short term fix here I noticed some linting errors in legacy code but did not include those in the PR (lmk if you'd like me to change that). LMK if there are any changes you'd like me to make to the PR. |
Btw, I'm happy to work on option B if you'd like. LMK. |
Please read this first
Describe the bug
When the upstream provider responds with an error payload (
{"error": …}
), the Agents SDK still returns aChatCompletion
-shaped object whosechoices
field isNone
.openai_chatcompletions.py
immediately dereferencesresponse.choices[0]
inside its debug logger, producingThe exception is raised inside the SDK, before the calling application sees the provider error.
Debug information
Repro steps
Run:
Observed output
(full traceback shows the line in
openai_chatcompletions.py
thatlogs
response.choices[0]
).Expected behavior
The SDK should detect that
response.choices
is missing and raise a clear domain-specific exception (e.g.ProviderError
) containing the provider’s error message, instead of crashing with an internalTypeError
.All downstream code paths would then have a chance to handle the error or retry gracefully.
The text was updated successfully, but these errors were encountered: