Skip to content

fix(server): preserve original LLM error in ExternalServiceAPIError#1362

Merged
sscargal merged 1 commit into
MemMachine:mainfrom
sscargal:bugfix/1309
Apr 21, 2026
Merged

fix(server): preserve original LLM error in ExternalServiceAPIError#1362
sscargal merged 1 commit into
MemMachine:mainfrom
sscargal:bugfix/1309

Conversation

@sscargal

Copy link
Copy Markdown
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)

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.

  • Unit Test

Checklist

  • I have signed the commit(s) within this pull request
  • My code follows the style guidelines of this project (See STYLE_GUIDE.md)
  • I have performed a self-review of my own code
  • I have commented my code
  • My changes generate no new warnings
  • I have added unit tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • Confirmed all checks passed
  • Contributor has signed the commit(s)
  • Reviewed the code
  • Run, Tested, and Verified the change(s) work as expected

Screenshots/Gifs

N/A

Further comments

None

  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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.OpenAIError string ({e}) in the non-retryable ExternalServiceAPIError message in OpenAIChatCompletionsLanguageModel.
  • Add a regression unit test asserting the wrapped error still matches "context length" for openai.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 edwinyyyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer-term fix should probably define a specific error type for context exceeded case.

@sscargal sscargal merged commit 458f4d9 into MemMachine:main Apr 21, 2026
48 checks passed
@sscargal sscargal deleted the bugfix/1309 branch April 21, 2026 23:53
@junttang

Copy link
Copy Markdown
Contributor

Thank you for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Context length errors from LLM are not detected, preventing split-and-retry mechanism for episodic summary

5 participants