Repository navigation
fix(realtime): treat credit_balance_exhausted as a fatal OpenAI error - #7819
Conversation
OpenAI Realtime can report an insufficient_quota error with code credit_balance_exhausted. The fatal check looks at the code before the type, so that error stayed recoverable: RealtimeModelFallbackAdapter left it to the plugin's reconnect instead of switching to the next model. Add the code to the fatal sets of both OpenAI realtime sessions (Realtime and GPT-Live).
main (livekit#7673) moved the GPT-Live session into livekit-agents/livekit/agents/llm/_realtime/gpt_live.py, so the credit_balance_exhausted fatal code now goes into that file's _FATAL_ERROR_CODES instead of the plugin's gpt_live_model.py.
| _FATAL_ERROR_CODES = frozenset( | ||
| { | ||
| "insufficient_quota", | ||
| "credit_balance_exhausted", |
There was a problem hiding this comment.
The root cause is a bit wider than the missing code. _is_fatal_error checks code or type, so when code is set, type is never looked at. insufficient_quota is already fatal, but here it is the type, not the code. In #6352 this same condition arrived as code='insufficient_quota'; OpenAI has since changed the code and kept the type. Adding credit_balance_exhausted covers today's code, and checking type as well would also cover any later change.
Could you change the check to match either field, e.g. code in _FATAL_ERROR_CODES or type in _FATAL_ERROR_CODES, in all three places that use the pattern?
llm/_realtime/openai.py(_is_fatal_error)llm/_realtime/gpt_live.py(_is_fatal_error)inference/realtime/openai.py(_is_fatal_error)
There was a problem hiding this comment.
Done in d1e5b3a: all three now match on either code or type, with tests for both cases.
_is_fatal_error used `code or type`, so the type was never looked at once a code was set. OpenAI has changed the code of the insufficient_quota error (livekit#6352 sent code='insufficient_quota', livekit#7805 sends code='credit_balance_exhausted') while keeping the type, so check both fields in llm/_realtime/openai.py, llm/_realtime/gpt_live.py and inference/realtime/openai.py.
Fixes #7805
OpenAI Realtime can send an
insufficient_quotaerror whose code iscredit_balance_exhausted:_is_fatal_errorreadscodebeforetype, andcredit_balance_exhaustedis not in_FATAL_ERROR_CODES, so this error is treated as recoverable.RealtimeModelFallbackAdapterthen leaves it to the plugin's reconnect instead of switching to the next model, which is the loop described in the issue.This adds
credit_balance_exhaustedto the fatal code set inllm/_realtime/openai.py(also used by the inference realtime session throughsuper()._is_fatal_error) and to the matching set in the GPT-Live session.It also changes
_is_fatal_errorto match either field (codein the set ortypein the set) instead ofcode or type, inllm/_realtime/openai.py,llm/_realtime/gpt_live.pyandinference/realtime/openai.py, so a fataltypestill stops the reconnect if OpenAI changes thecodeagain. A missing or emptycode/typenever matches.Tests:
test_is_fatal_error_matches_known_codesnow covers thecode="credit_balance_exhausted", type="insufficient_quota"shape.test_an_exhausted_credit_balance_is_not_recoverableintests/test_gpt_live_model.pychecks that the GPT-Live error event raises a non-retryableAPIError.test_is_fatal_error_matches_either_code_or_type,test_fatal_errors_match_either_code_or_type(GPT-Live) andtest_gateway_fatal_errors_match_either_code_or_type(inference) check that a fataltypewith an unlistedcodeis fatal, and that unlisted,Noneor empty values on both fields are not.Each of these tests fails without the change (the new ones on their fatal-
type-with-unlisted-codecases) and passes with it.Ran locally:
uv run pytest tests/test_realtime/ --unit(125 passed),uv run pytest tests/test_gpt_live_model.py --unit(111 passed),ruff check/ruff format --checkon the changed files,uv run mypy --platform linux -p livekit.agents(no issues). Before the either-field change, a fulluv run pytest --unit --audio_eot -n 4here had failures only intest_room.py(no docker),test_vad_buffer_resize.py,test_tokenizer.py(missing nltk data) and an import error intest_plugin_azure_realtime.py(azure.ai.voicelivenot installed); the room and VAD tests fail the same way onmainin this environment. I did not test against a real OpenAI account with no credits.