fix(copilot_auth): tolerate missing object/index in chat completions - #23
Open
snair20-1984 wants to merge 5 commits into
Open
fix(copilot_auth): tolerate missing object/index in chat completions#23snair20-1984 wants to merge 5 commits into
snair20-1984 wants to merge 5 commits into
Conversation
GitHub Copilot's /chat/completions returns a body that is almost OpenAI-shaped but omits two purely cosmetic fields: the top-level "object": "chat.completion" discriminator and the per-choice "index". The OpenAI SDK builds ChatCompletion via model_construct and performs no validation, so both fields silently arrive as None. pydantic-ai then runs a strict _ChatCompletion.model_validate() in _process_response and raises UnexpectedModelBehavior, discarding a response whose message content was perfectly valid. This only affects non-streaming calls -- the streaming path parses lenient ChatCompletionChunks -- so normal chat was unaffected while /compact failed every time on Copilot models. Compaction reported success with 0% token reduction because the summariser is the only bare agent.run() call in the codebase. Subclass OpenAIChatModel and backfill the two fields via _validate_completion, which pydantic-ai documents as the hook for custom completion validation. Existing non-None values are never overwritten, so a spec-compliant response passes through untouched, and any future non-streaming caller is covered by the same seam. Affects all Copilot models routed through Chat Completions, not just the Claude family.
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.
GitHub Copilot's /chat/completions returns a body that is almost OpenAI-shaped but omits two purely cosmetic fields: the top-level "object": "chat.completion" discriminator and the per-choice "index".
The OpenAI SDK builds ChatCompletion via model_construct and performs no validation, so both fields silently arrive as None. pydantic-ai then runs a strict _ChatCompletion.model_validate() in _process_response and raises UnexpectedModelBehavior, discarding a response whose message content was perfectly valid.
This only affects non-streaming calls -- the streaming path parses lenient ChatCompletionChunks -- so normal chat was unaffected while /compact failed every time on Copilot models. Compaction reported success with 0% token reduction because the summariser is the only bare agent.run() call in the codebase.
Subclass OpenAIChatModel and backfill the two fields via _validate_completion, which pydantic-ai documents as the hook for custom completion validation. Existing non-None values are never overwritten, so a spec-compliant response passes through untouched, and any future non-streaming caller is covered by the same seam.
Affects all Copilot models routed through Chat Completions, not just the Claude family.
Fixes #22