fix(openai): match longest model prefix for context window size#6603
Open
Kropiunig wants to merge 1 commit into
Open
fix(openai): match longest model prefix for context window size#6603Kropiunig wants to merge 1 commit into
Kropiunig wants to merge 1 commit into
Conversation
OpenAICompletion.get_context_window_size iterated the prefix table in insertion order and returned on the first match. Because "gpt-4" precedes the more specific "gpt-4o", "gpt-4o-mini", "gpt-4-turbo" and "gpt-4.1" entries, every "gpt-4*" model (including the default "gpt-4o") resolved to gpt-4's 8192-token window instead of its own, making context management far too aggressive. Sort the prefixes by length (longest first) before matching, mirroring the AzureCompletion provider which already does this. Add a regression test covering gpt-4, gpt-4o and gpt-4o-mini.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughOpenAI context-window lookup now prioritizes the longest matching model prefix. A regression test covers ChangesOpenAI context window matching
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Summary
OpenAICompletion.get_context_window_size()iterates the model-prefix table in insertion order and returns on the firststartswithmatch:Because
"gpt-4"comes first and"gpt-4o".startswith("gpt-4")isTrue, everygpt-4*model collapses to gpt-4's 8192-token window before its own, more-specific entry is ever reached. The more specific keys (gpt-4o,gpt-4o-mini,gpt-4-turbo,gpt-4.1, ...) are effectively dead code.This hits the default model
gpt-4o(model: str = "gpt-4o"), so out of the boxget_context_window_size()returnsint(8192 * 0.85) = 6963instead ofint(128000 * 0.85) = 108800— making summarization/truncation far too aggressive and causing spurious context-window-exceeded handling.Fix
Sort the prefixes by length (longest first) before matching, so the most specific prefix wins. This mirrors
AzureCompletion.get_context_window_size(), which already does exactly this:Test
Adds
test_openai_context_window_uses_longest_prefix_match, assertinggpt-4o-> 128k andgpt-4o-mini-> 200k windows whilegpt-4stays at 8k. It fails onmain(6963 == 108800AssertionError) and passes with this change. Existing behavior for models without a shorter conflicting prefix (e.g.o3-mini-> 200k) is unchanged.