Skip to content

Conversation

@jhamon
Copy link
Collaborator

@jhamon jhamon commented Jan 28, 2026

Summary

Add query model classes for the document search API.

Closes SDK-108

Classes

Class Purpose Key Parameters
TextQuery Full-text search field, query, boost, slop
VectorQuery Vector similarity search field, values, sparse_values

Both classes:

  • Support dict-like access (query["field"])
  • Serialize to API format via to_dict()
  • Can be used as score_by parameter in search_documents()

Usage Example

from pinecone import TextQuery, VectorQuery

# Text query for full-text search
results = index.search_documents(
    namespace="movies",
    score_by=TextQuery(field="title", query='return "pink panther"'),
    top_k=10,
)

# Vector query for similarity search
results = index.search_documents(
    namespace="movies",
    score_by=VectorQuery(field="embedding", values=[0.1, 0.2, 0.3, ...]),
    top_k=10,
)

Test Plan

  • Unit tests for TextQuery serialization (7 tests)
  • Unit tests for VectorQuery serialization (7 tests)
  • Tests for dict-like access
  • Tests for optional parameters
  • mypy type checking passes

Note

Low Risk
Low risk: adds new lightweight dataclass helpers and exports plus unit tests, without changing existing request/response handling or core search logic.

Overview
Adds new TextQuery and VectorQuery dataclasses to represent search_documents() score_by inputs, including optional parameters and to_dict() serialization (and DictLike access via the existing base).

Exports these types via pinecone.db_data.dataclasses and top-level lazy imports in pinecone/__init__.py, and adds unit tests validating serialization and dict-like behavior.

Written by Cursor Bugbot for commit f963847. This will update automatically on new commits. Configure here.

Add query model classes for the document search API:
- TextQuery: for full-text search with field, query, boost, slop
- VectorQuery: for vector search with field, values, sparse_values

Both classes support dict-like access and serialize to API format.

Closes SDK-108
@jhamon jhamon added the enhancement New feature or request label Jan 28, 2026
@jhamon jhamon merged commit 0cf3d72 into fts Jan 28, 2026
7 checks passed
@jhamon jhamon deleted the jhamon/sdk-108-query-model-classes branch January 28, 2026 19:54
Copy link

@cursor cursor bot left a 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 3 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.


:returns: Dictionary representation for the API.
"""
result: dict = {"field": self.field, "query": self.query}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextQuery.to_dict() uses wrong API field name

High Severity

The to_dict() method serializes the query string with key "query", but the Pinecone API expects "text_query" as shown in the OpenAPI-generated model at pinecone/core/openapi/db_data/model/text_query.py. This mismatch will cause API requests using TextQuery as the score_by parameter to fail or produce unexpected results because the server won't recognize the query text.

Fix in Cursor Fix in Web

result["boost"] = self.boost
if self.slop is not None:
result["slop"] = self.slop
return result
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextQuery.to_dict() missing required type discriminator field

High Severity

The to_dict() methods in TextQuery and VectorQuery omit the required type discriminator field. The Pinecone API uses this field (e.g., 'text', 'vector') to correctly identify the query type within the score_by parameter, so its absence prevents proper API interpretation.

Additional Locations (1)

Fix in Cursor Fix in Web

boost: float | None = None
slop: int | None = None

def to_dict(self) -> dict:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent method naming with similar query classes

Low Severity

The new TextQuery and VectorQuery classes use to_dict() for serialization, while semantically similar query classes in the same module (SearchQuery, SearchQueryVector, SearchRerank) all use as_dict(). This naming inconsistency within the query-related classes could confuse developers expecting a uniform API.

Additional Locations (1)

Fix in Cursor Fix in Web

jhamon added a commit that referenced this pull request Jan 28, 2026
Address unresolved review comments from PR #586:

- Fix TextQuery: use "text_query" instead of "query" for API field name
- Add required "type" discriminator field ("text" / "vector")
- Rename to_dict() to as_dict() for consistency with SearchQuery classes
jhamon added a commit that referenced this pull request Jan 28, 2026
## Summary

Fixes unresolved review comments from PR #586 that were merged without
being addressed.

**Related PR:** #586

## Problems Fixed

### 1. Wrong API field name in `TextQuery` (High Severity)
The `as_dict()` method was serializing the query string with key
`"query"`, but the Pinecone API expects `"text_query"` as defined in the
OpenAPI spec.

### 2. Missing `type` discriminator field (High Severity)
Both `TextQuery` and `VectorQuery` were missing the required `type`
discriminator field used by the `ScoreByQuery` oneOf schema:
- `TextQuery` → `"type": "text"`
- `VectorQuery` → `"type": "vector"`

### 3. Inconsistent method naming (Low Severity)
Changed `to_dict()` to `as_dict()` for consistency with similar
search-related classes (`SearchQuery`, `SearchQueryVector`,
`SearchRerank`).

## Breaking Changes

Since these classes were just added in PR #586 and haven't been released
yet, these changes should not affect any users:
- `TextQuery.to_dict()` → `TextQuery.as_dict()`
- `VectorQuery.to_dict()` → `VectorQuery.as_dict()`

## Usage Example

```python
from pinecone import TextQuery, VectorQuery

# TextQuery for full-text search
text_query = TextQuery(field="title", query="pink panther")
text_query.as_dict()
# Returns: {"type": "text", "field": "title", "text_query": "pink panther"}

# With optional parameters
text_query = TextQuery(field="title", query="pink panther", boost=2.0, slop=1)
text_query.as_dict()
# Returns: {"type": "text", "field": "title", "text_query": "pink panther", "boost": 2.0, "slop": 1}

# VectorQuery for similarity search
vector_query = VectorQuery(field="embedding", values=[0.1, 0.2, 0.3])
vector_query.as_dict()
# Returns: {"type": "vector", "field": "embedding", "values": [0.1, 0.2, 0.3]}

# Use with search_documents()
results = index.search_documents(
    namespace="movies",
    score_by=TextQuery(field="title", query="pink panther"),
    top_k=10,
)
```

## Test Plan

- [x] Updated unit tests to verify correct API format with `type`
discriminator
- [x] Updated tests to use `as_dict()` method name
- [x] All 16 query class tests pass
- [x] mypy type checking passes

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Medium risk because it changes the serialized request shape and
renames a public method (`to_dict()` → `as_dict()`), which can break any
existing callers even though the change is localized.
> 
> **Overview**
> Fixes `TextQuery` and `VectorQuery` API serialization used by
`score_by` by adding the required `type` discriminator
(`"text"`/`"vector"`) and aligning field names (notably `query` →
`text_query` for text scoring).
> 
> Renames the serialization helper from `to_dict()` to `as_dict()` on
both classes and updates unit tests to assert the new payload format and
method name.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
50ee953. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants