Implementation:AI Northstar Tech Vector io VespaClient
| Knowledge Sources | |
|---|---|
| Domains | Vector_Database, Vespa, HTTP_Client, Error_Handling |
| Last Updated | 2026-10-15 00:00 GMT |
Overview
Concrete tool for HTTP communication with Vespa document API, providing document retrieval with pagination and a structured error hierarchy, built on the httpx library.
Description
The VespaClient class provides a custom HTTP client for communicating with the Vespa document API. It uses httpx.Client with configurable connection pools and SSL/certificate-based authentication. The primary method, get_all_documents, fetches documents from a Vespa schema via the Document v1 API with support for streaming and continuation-token-based pagination.
The module also defines a Pydantic-based response model hierarchy: Document (single document with fields) and VisitDocumentsResponse (batch response with documents, count, and continuation token).
The error hierarchy uses a MarqoErrorMeta metaclass that auto-generates __init__ methods for exception subclasses. MarqoError is the base class, VespaError wraps HTTP/SSL errors, and VespaStatusError adds a status_code property extracted from the HTTP response.
Usage
Use VespaClient when you need to retrieve documents from a Vespa instance via the Document v1 API. It is used internally by ExportVespa for the Vespa export workflow.
Code Reference
Source Location
- Repository: vector-io
- File: src/vdf_io/marqo_vespa_util.py
- Lines: 1-170
Signature
class Document(BaseModel):
id: str
fields: Dict[str, Any]
class VisitDocumentsResponse(BaseModel):
path_id: str = Field(alias="pathId")
documents: List[Document]
document_count: int = Field(alias="documentCount")
continuation: Optional[str]
class VespaClient:
def __init__(
self,
config_url: str,
document_url: str,
query_url: str,
content_cluster_name: str,
cert_file: Optional[str] = None,
pk_file: Optional[str] = None,
pool_size: int = 10,
feed_pool_size: int = 10,
get_pool_size: int = 10,
delete_pool_size: int = 10,
partial_update_pool_size: int = 10,
):
"""Create a VespaClient with httpx.Client and SSL/cert support."""
def get_all_documents(
self, schema: str, stream: bool = False,
continuation: Optional[str] = None,
) -> VisitDocumentsResponse:
"""Fetch all documents in a schema via Vespa Document v1 API."""
def _raise_for_status(self, resp: httpx.Response) -> None:
"""Raise structured VespaError on HTTP status errors."""
class MarqoErrorMeta(type):
"""Metaclass that auto-generates __init__(message, cause) for subclasses."""
class MarqoError(Exception, metaclass=MarqoErrorMeta):
"""Base class for all Marqo/Vespa errors."""
class VespaError(MarqoError):
"""Wraps httpx.HTTPError and ssl.SSLError."""
class VespaStatusError(VespaError):
"""HTTP status error with status_code property."""
Import
from vdf_io.marqo_vespa_util import VespaClient, VespaError, VespaStatusError
from vdf_io.marqo_vespa_util import Document, VisitDocumentsResponse
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config_url | str | Yes | Vespa Deploy API base URL |
| document_url | str | Yes | Vespa Document API base URL |
| query_url | str | Yes | Vespa Query API base URL |
| content_cluster_name | str | Yes | Vespa content cluster name |
| cert_file | str | No | Path to certificate file for SSL auth |
| pk_file | str | No | Path to private key file (required with cert_file) |
| pool_size | int | No | Max keepalive connections (default: 10) |
| schema | str | Yes (get_all_documents) | Schema name to fetch documents from |
| stream | bool | No (get_all_documents) | Enable streaming response (default: False) |
| continuation | str | No (get_all_documents) | Pagination continuation token |
Outputs
| Name | Type | Description |
|---|---|---|
| VisitDocumentsResponse | VisitDocumentsResponse | Pydantic model with documents, document_count, continuation |
| VespaError | Exception | Raised on HTTP/SSL errors |
| VespaStatusError | Exception | Raised on HTTP status errors with status_code property |
Usage Examples
Basic Document Retrieval
from vdf_io.marqo_vespa_util import VespaClient
# 1. Create a VespaClient (no SSL)
client = VespaClient(
config_url="http://localhost:8080",
document_url="http://localhost:8080",
query_url="http://localhost:8080",
content_cluster_name="my_cluster",
)
# 2. Fetch all documents from a schema
response = client.get_all_documents(schema="my_schema", stream=True)
print(f"Retrieved {response.document_count} documents")
# 3. Handle pagination with continuation token
if response.continuation:
next_response = client.get_all_documents(
schema="my_schema",
stream=True,
continuation=response.continuation,
)
Certificate-Based Authentication
from vdf_io.marqo_vespa_util import VespaClient, VespaError
# 1. Create a VespaClient with cert/key
client = VespaClient(
config_url="https://my-vespa.vespacloud.net:4443",
document_url="https://my-vespa.vespacloud.net:8080",
query_url="https://my-vespa.vespacloud.net:8080",
content_cluster_name="my_cluster",
cert_file="/path/to/cert.pem",
pk_file="/path/to/private_key.pem",
pool_size=20,
)
# 2. Fetch documents with error handling
try:
response = client.get_all_documents(schema="articles", stream=True)
for doc in response.documents:
print(doc.id, doc.fields)
except VespaError as e:
print(f"Vespa error: {e.message}")
Related Pages
Implements Principle
Requires Environment
Related Implementations
- Used by Implementation:AI_Northstar_Tech_Vector_io_ExportVespa for HTTP document retrieval