Don't crash BYOK/custom models when the response hits the length limit#324121
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #319592, where custom/BYOK OpenAI-compatible endpoints that return finish_reason: "length" (i.e. hit their context window) caused the Copilot language-model wrapper to throw Error: Response too long. and discard all text the model had already streamed. The fix intercepts the ChatFetchResponseType.Length result in CopilotLanguageModelWrapper._provideLanguageModelResponse and resolves gracefully (returning undefined) instead of throwing, since the partial text was already reported to the consumer via the streaming finishedCb. A warning is logged, and a unit test verifies the truncated response no longer surfaces as a hard failure.
Changes:
- Treat a
Lengthfinish reason as a successful (truncated) response rather than throwing, keeping the already-streamed partial text. - Log a warning when a response is truncated due to the length limit.
- Add a unit test asserting
provideLanguageModelResponsedoes not throw on aLengthresponse.
Show a summary per file
| File | Description |
|---|---|
| extensions/copilot/src/extension/conversation/vscode-node/languageModelAccess.ts | Adds an early return for ChatFetchResponseType.Length before the failure path, logging a warning and returning undefined instead of throwing "Response too long." |
| extensions/copilot/src/extension/conversation/vscode-node/test/languageModelAccess.test.ts | Adds a "length finish reason" suite verifying that a truncated (Length) response does not throw. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Medium
dmitrivMS
approved these changes
Jul 3, 2026
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.
Fixes #319592
When a custom/BYOK model returns
finish_reason: "length"(hit its contextwindow), we threw
Error: Response too long.and discarded everything the modelalready streamed.
By the time we get the
Lengthresult the partial text has already beenstreamed via the finished callback, so instead of throwing we just resolve and
keep it. This matches how the rest of the codebase already handles
Length(
defaultIntentRequestHandler,agentIntent,codeMapperall use the partialtext rather than failing).