fix(server): preserve original LLM error in ExternalServiceAPIError#1362
Merged
Conversation
The non-retryable branch in OpenAIChatCompletionsLanguageModel wrapped the
OpenAI error with a generic "due to non-retryable BadRequestError" message
and dropped str(e). That stripped the keywords ("context length", "token
limit", …) that ShortTermMemoryConsolidator._is_exceed_context_window_error
relies on, so the split-and-retry path never triggered and episodic
summaries silently failed on oversized prompts.
Append str(e) to the wrapper message so the detector sees the original
text and recursion kicks in as designed. Adds a regression test that
raises openai.BadRequestError("…context length is only 202752 tokens…")
and asserts the wrapped ExternalServiceAPIError still matches "context
length".
Refs: MemMachine#1309, MemMachine#1247
Signed-off-by: Steve Scargall <37674041+sscargal@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes episodic summarization’s split-and-retry detection by preserving the original OpenAI error text inside ExternalServiceAPIError, ensuring _is_exceed_context_window_error() can still match keywords like “context length”.
Changes:
- Include the original
openai.OpenAIErrorstring ({e}) in the non-retryableExternalServiceAPIErrormessage inOpenAIChatCompletionsLanguageModel. - Add a regression unit test asserting the wrapped error still matches
"context length"foropenai.BadRequestError.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/server/src/memmachine_server/common/language_model/openai_chat_completions_language_model.py | Appends the original OpenAI exception text to the non-retryable wrapper error message. |
| packages/server/server_tests/memmachine_server/common/language_model/test_openai_chat_completions_language_model.py | Adds a regression test ensuring the wrapped error preserves “context length” text. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
edwinyyyu
approved these changes
Apr 21, 2026
edwinyyyu
left a comment
Contributor
There was a problem hiding this comment.
Longer-term fix should probably define a specific error type for context exceeded case.
jealous
approved these changes
Apr 21, 2026
Contributor
|
Thank you for the fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose of the change
preserve original LLM error in ExternalServiceAPIError
Description
The non-retryable branch in OpenAIChatCompletionsLanguageModel wrapped the
OpenAI error with a generic "due to non-retryable BadRequestError" message
and dropped str(e). That stripped the keywords ("context length", "token
limit", …) that ShortTermMemoryConsolidator._is_exceed_context_window_error
relies on, so the split-and-retry path never triggered and episodic
summaries silently failed on oversized prompts.
Append str(e) to the wrapper message so the detector sees the original
text and recursion kicks in as designed. Adds a regression test that
raises openai.BadRequestError("…context length is only 202752 tokens…")
and asserts the wrapped ExternalServiceAPIError still matches "context
length".
Fixes/Closes
Fixes #1309
Refs: #1247
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.
Checklist
Maintainer Checklist
Screenshots/Gifs
N/A
Further comments
None