fix(python): isolate per-entry parse failures in list_models() (#1302)#1303
Open
jrob5756 wants to merge 2 commits into
Open
fix(python): isolate per-entry parse failures in list_models() (#1302)#1303jrob5756 wants to merge 2 commits into
jrob5756 wants to merge 2 commits into
Conversation
Wrap each ModelInfo.from_dict(model) call in a try/except so a single malformed entry in the models.list response (e.g. backwards-incompatible schema drift on one model) is logged at WARNING level and skipped, instead of raising and taking down list_models() for every consumer. Custom on_list_models handlers are unaffected; only the RPC path is hardened. Adds unit tests covering: skip-and-return-valid, all-malformed-empty-list, empty-payload, and non-dict entries. Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Hardens the Python SDK’s CopilotClient.list_models() parsing so a single malformed model entry in the models.list JSON-RPC response won’t fail the entire call (and leave the cache empty), addressing the resilience gap described in #1302.
Changes:
- Update
CopilotClient.list_models()to parse model entries one-by-one, logging a warning and skipping malformed entries instead of failing the whole call. - Update the
list_models()docstring to document the new “skip malformed entries” behavior. - Add unit tests validating partial/malformed payload handling (including non-dict entries) without spawning the CLI.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/copilot/client.py | Implements per-entry try/except parsing in list_models() and documents the new resilience behavior. |
| python/test_client.py | Adds targeted unit tests using a fake RPC client to verify malformed entries are skipped with warnings. |
Per review on PR github#1303 — `except Exception` is too broad; it masks unexpected programmer errors in ModelInfo.from_dict (or its helpers) by silently skipping entries. - Pre-check isinstance(model_data, dict) before parsing, with its own warning for non-dict entries. - Narrow the parse-failure handler to (ValueError, TypeError, AssertionError) — the shape-related exceptions ModelInfo.from_dict and its helpers actually raise. Other exceptions propagate. - Add test_unexpected_exceptions_propagate covering the new behavior. Co-authored-by: Copilot <[email protected]>
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 #1302 (option 2 — per-entry isolation hardening).
Summary
CopilotClient.list_models()parses themodels.listresponse with alist comprehension:
A single malformed entry raises and the whole call fails — the cache stays
empty, so every retry from a fresh session fails identically. The original
multiplier-requiredValueErrorfrom #1302 is already fixed inmain, but the bot that triaged the issue endorsed implementingoption 2
as a follow-up so future schema drift on individual models can't take down
list_models()for every consumer.What changed
python/copilot/client.py— wrap eachModelInfo.from_dict(model)intry/exceptinside the RPC path:WARNING(with model id when available) for each malformed entry.on_list_modelshandlers are unaffected.Docstring updated to document the new resilience behavior.
Why Python only
as { models: ModelInfo[] }) — no eager parse.json.Unmarshal— missing fields become Go zero values.Only the Python SDK has a hand-rolled per-entry parser that raises on
missing fields, so per-entry isolation is the Python-specific gap.
Tests
New
TestListModelsParserResilienceclass inpython/test_client.pywith 4 unit tests:test_skips_malformed_entry_and_returns_valid_ones— mixed payload returns only the valid models, with one warning per skipped entry.test_all_malformed_returns_empty_list_without_raising— all-broken payload returns[]instead of raising.test_empty_models_payload_returns_empty_list— empty payload still returns[].test_non_dict_entry_is_skipped_with_warning— non-dict entries (string, int) are skipped without crashing.Tests use a small
_FakeRpcClientstub so they don't need to spawn the CLI subprocess.Validation
ty checkreports the same 37 pre-existing diagnostics onmainand on this branch — no new type errors introduced.Compatibility
list[ModelInfo].on_list_modelshandlers behave identically.is still cached; the cache is still cleared on disconnect.