-
Notifications
You must be signed in to change notification settings - Fork 115
feat: add Protocol definitions for adapter layer #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Protocol definitions for adapter layer #604
Conversation
Add formal Protocol interfaces that define the contract between OpenAPI models and SDK adapters. This improves type safety, documentation, and maintainability without any breaking changes. - Created pinecone/adapters/protocols.py with 5 Protocol definitions - Updated adapter functions to use protocol type annotations - Added 13 unit tests verifying protocol compliance - All tests pass, mypy validates protocol usage Related: SDK-275 Co-authored-by: Cursor <cursoragent@cursor.com>
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
Changed FetchResponseAdapter.namespace from `str` to `str | None` to correctly reflect that the OpenAPI FetchResponse model marks namespace as optional. Updated the adapter function to handle None namespace by converting it to empty string, consistent with QueryResponse adapter. - Updated protocol type annotation - Added null-coalescing in adapter function - Added test for None namespace case Fixes Cursor Bugbot issue Co-authored-by: Cursor <cursoragent@cursor.com>
|
Fixed in commit eb0b688. Changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| """ | ||
|
|
||
| namespace: str | None | ||
| vectors: dict[str, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Protocol incorrectly marks vectors as required when API allows None
Medium Severity
The FetchResponseAdapter protocol defines vectors: dict[str, Any] as a required attribute, but the OpenAPI spec marks vectors as [optional]. If the API returns a response without vectors (or with null), the adapter code at openapi_response.vectors.items() will crash with an AttributeError. The protocol type should be dict[str, Any] | None to match the actual API contract and prompt defensive handling.


Problem
The adapter layer lacked explicit interface definitions between generated OpenAPI models and SDK code. This made it unclear what properties and methods SDK code depends on from the OpenAPI models, reducing type safety and making refactoring more error-prone.
Without formal protocols, developers had to:
Anytypes or implicit duck typingSolution
This PR adds formal
Protocolinterfaces that define the contract between OpenAPI models and SDK adapters. Protocols are Python's way of defining structural subtyping (similar to interfaces in other languages), enabling static type checking while maintaining flexibility.Key Architecture Decisions
typing.Protocolto define explicit interfaces without requiring inheritanceProtocols Added
Five protocols were created in
pinecone/adapters/protocols.py:QueryResponseAdapter: Interface for OpenAPI QueryResponse objectsUpsertResponseAdapter: Interface for OpenAPI UpsertResponse objectsFetchResponseAdapter: Interface for OpenAPI FetchResponse objectsIndexModelAdapter: Interface for OpenAPI IndexModel objectsIndexStatusAdapter: Interface for IndexModelStatus objectsUsage Example
The protocols enable better type safety in adapter functions:
For SDK users, nothing changes. The adapter functions work exactly as before:
Breaking Changes
None. This is a non-breaking internal improvement.
Testing
Unit Tests
tests/unit/adapters/test_protocols.pyverify protocol complianceType Safety
mypyvalidates protocol usage with no errors in adapters moduleQuality Checks
Follow-up Items
None required. This is a complete, self-contained improvement.
Potential future enhancements (not required):
Related
Documentation
The protocols are fully documented with docstrings explaining:
Module-level documentation in
pinecone/adapters/protocols.pyexplains the benefits and usage patterns.Made with Cursor
Note
Medium Risk
Mostly type-safety and test additions, but it also changes
adapt_fetch_responseto normalizenamespace=Noneto an empty string, which is a small runtime behavior change that could affect edge-case callers.Overview
Adds a new
pinecone.adapters.protocolsmodule definingProtocolcontracts for the OpenAPI models consumed by the adapter layer, and re-exports these protocols frompinecone.adapters.Updates
adapt_query_response,adapt_upsert_response, andadapt_fetch_responseto accept protocol-typed inputs instead ofAny, and normalizes fetchnamespaceto""when the OpenAPI response providesNone.Introduces unit tests that assert real generated OpenAPI models satisfy the new protocols, plus a new adapter test covering the
None-namespace fetch case.Written by Cursor Bugbot for commit eb0b688. This will update automatically on new commits. Configure here.