Skip to content

Application

vespa.application

Vespa(url, port=None, deployment_message=None, cert=None, key=None, vespa_cloud_secret_token=None, output_file=sys.stdout, application_package=None)

Bases: object

Establish a connection with an existing Vespa application.

Parameters:

Name Type Description Default
url str

Vespa endpoint URL.

required
port int

Vespa endpoint port.

None
deployment_message str

Message returned by Vespa engine after deployment. Used internally by deploy methods.

None
cert str

Path to data plane certificate and key file in case the 'key' parameter is None. If 'key' is not None, this should be the path of the certificate file. Typically generated by Vespa-cli with 'vespa auth cert'.

None
key str

Path to the data plane key file. Typically generated by Vespa-cli with 'vespa auth cert'.

None
vespa_cloud_secret_token str

Vespa Cloud data plane secret token.

None
output_file str

Output file to write output messages.

stdout
application_package str

Application package definition used to deploy the application.

None
Example usage
Vespa(url="https://cord19.vespa.ai")   # doctest: +SKIP

Vespa(url="http://localhost", port=8080)
Vespa(http://localhost, 8080)

Vespa(url="https://token-endpoint..z.vespa-app.cloud", vespa_cloud_secret_token="your_token")  # doctest: +SKIP

Vespa(url="https://mtls-endpoint..z.vespa-app.cloud", cert="/path/to/cert.pem", key="/path/to/key.pem")  # doctest: +SKIP

application_package property

Get application package definition, if available.

asyncio(connections=1, total_timeout=None, timeout=httpx.Timeout(5), **kwargs)

Access Vespa asynchronous connection layer. Should be used as a context manager.

Example usage
async with app.asyncio() as async_app:
    response = await async_app.query(body=body)

# passing kwargs
limits = httpx.Limits(max_keepalive_connections=5, max_connections=5, keepalive_expiry=15)
timeout = httpx.Timeout(connect=3, read=4, write=2, pool=5)
async with app.asyncio(connections=5, timeout=timeout, limits=limits) as async_app:
    response = await async_app.query(body=body)

See VespaAsync for more details on the parameters.

Parameters:

Name Type Description Default
connections int

Number of maximum_keepalive_connections.

1
total_timeout int

Deprecated. Will be ignored. Use timeout instead.

None
timeout Timeout

httpx.Timeout object. See Timeouts. Defaults to 5 seconds.

Timeout(5)
**kwargs dict

Additional arguments to be passed to the httpx.AsyncClient.

{}

Returns:

Name Type Description
VespaAsync VespaAsync

Instance of Vespa asynchronous layer.

syncio(connections=8, compress='auto')

Access Vespa synchronous connection layer. Should be used as a context manager.

Example usage:

```python
with app.syncio() as sync_app:
    response = sync_app.query(body=body)
```

See for more details.

Parameters:

Name Type Description Default
connections int

Number of allowed concurrent connections.

8
total_timeout float

Total timeout in seconds.

required
compress Union[str, bool]

Whether to compress the request body. Defaults to "auto", which will compress if the body is larger than 1024 bytes.

'auto'

Returns:

Name Type Description
VespaAsyncLayer VespaSync

Instance of Vespa asynchronous layer.

wait_for_application_up(max_wait=300)

Wait for application endpoint ready (/ApplicationStatus).

Parameters:

Name Type Description Default
max_wait int

Seconds to wait for the application endpoint.

300

Raises:

Type Description
RuntimeError

If not able to reach endpoint within max_wait or the client fails to authenticate.

Returns:

Type Description
None

None

get_application_status()

Get application status (/ApplicationStatus).

Returns:

Type Description
Optional[Response]

None

get_model_endpoint(model_id=None)

Get stateless model evaluation endpoints.

query(body=None, groupname=None, **kwargs)

Send a query request to the Vespa application.

Send 'body' containing all the request parameters.

Parameters:

Name Type Description Default
body dict

Dictionary containing request parameters.

None
groupname str

The groupname used with streaming search.

None
**kwargs dict

Extra Vespa Query API parameters.

{}

Returns:

Type Description
VespaQueryResponse

The response from the Vespa application.

feed_data_point(schema, data_id, fields, namespace=None, groupname=None, compress='auto', **kwargs)

Feed a data point to a Vespa app. Will create a new VespaSync with connection overhead.

Example usage
app = Vespa(url="localhost", port=8080)
data_id = "1",
fields = {
        "field1": "value1",
    }
with VespaSync(app) as sync_app:
    response = sync_app.feed_data_point(
        schema="schema_name",
        data_id=data_id,
        fields=fields
    )
print(response)

Parameters:

Name Type Description Default
schema str

The schema that we are sending data to.

required
data_id str

Unique id associated with this data point.

required
fields dict

Dictionary containing all the fields required by the schema.

required
namespace str

The namespace that we are sending data to.

None
groupname str

The groupname that we are sending data to.

None
compress Union[str, bool]

Whether to compress the request body. Defaults to "auto", which will compress if the body is larger than 1024 bytes.

'auto'

Returns:

Name Type Description
VespaResponse VespaResponse

The response of the HTTP POST request.

feed_iterable(iter, schema=None, namespace=None, callback=None, operation_type='feed', max_queue_size=1000, max_workers=8, max_connections=16, compress='auto', **kwargs)

Feed data from an Iterable of Dict with the keys 'id' and 'fields' to be used in the feed_data_point function.

Uses a queue to feed data in parallel with a thread pool. The result of each operation is forwarded to the user-provided callback function that can process the returned VespaResponse.

Example usage
app = Vespa(url="localhost", port=8080)
data = [
    {"id": "1", "fields": {"field1": "value1"}},
    {"id": "2", "fields": {"field1": "value2"}},
]
def callback(response, id):
    print(f"Response for id {id}: {response.status_code}")
app.feed_iterable(data, schema="schema_name", callback=callback)

Parameters:

Name Type Description Default
iter Iterable[dict]

An iterable of Dict containing the keys 'id' and 'fields' to be used in the feed_data_point. Note that this 'id' is only the last part of the full document id, which will be generated automatically by pyvespa.

required
schema str

The Vespa schema name that we are sending data to.

None
namespace str

The Vespa document id namespace. If no namespace is provided, the schema is used.

None
callback function

A callback function to be called on each result. Signature callback(response: VespaResponse, id: str).

None
operation_type str

The operation to perform. Defaults to feed. Valid values are feed, update, or delete.

'feed'
max_queue_size int

The maximum size of the blocking queue and max in-flight operations.

1000
max_workers int

The maximum number of workers in the threadpool executor.

8
max_connections int

The maximum number of persisted connections to the Vespa endpoint.

16
compress Union[str, bool]

Whether to compress the request body. Defaults to "auto", which will compress if the body is larger than 1024 bytes.

'auto'
**kwargs dict

Additional parameters passed to the respective operation type specific function (_data_point).

{}

Returns:

Type Description

None

feed_async_iterable(iter, schema=None, namespace=None, callback=None, operation_type='feed', max_queue_size=1000, max_workers=64, max_connections=1, **kwargs)

Feed data asynchronously using httpx.AsyncClient with HTTP/2. Feed from an Iterable of Dict with the keys 'id' and 'fields' to be used in the feed_data_point function. The result of each operation is forwarded to the user-provided callback function that can process the returned VespaResponse. Prefer using this method over feed_iterable when the operation is I/O bound from the client side.

Example usage
app = Vespa(url="localhost", port=8080)
data = [
    {"id": "1", "fields": {"field1": "value1"}},
    {"id": "2", "fields": {"field1": "value2"}},
]
async def callback(response, id):
    print(f"Response for id {id}: {response.status_code}")
app.feed_async_iterable(data, schema="schema_name", callback=callback)

Parameters:

Name Type Description Default
iter Iterable[dict]

An iterable of Dict containing the keys 'id' and 'fields' to be used in the feed_data_point. Note that this 'id' is only the last part of the full document id, which will be generated automatically by pyvespa.

required
schema str

The Vespa schema name that we are sending data to.

None
namespace str

The Vespa document id namespace. If no namespace is provided, the schema is used.

None
callback function

A callback function to be called on each result. Signature callback(response: VespaResponse, id: str).

None
operation_type str

The operation to perform. Defaults to feed. Valid values are feed, update, or delete.

'feed'
max_queue_size int

The maximum number of tasks waiting to be processed. Useful to limit memory usage. Default is 1000.

1000
max_workers int

Maximum number of concurrent requests to have in-flight, bound by an asyncio.Semaphore, that needs to be acquired by a submit task. Increase if the server is scaled to handle more requests.

64
max_connections int

The maximum number of connections passed to httpx.AsyncClient to the Vespa endpoint. As HTTP/2 is used, only one connection is needed.

1
**kwargs dict

Additional parameters passed to the respective operation type-specific function (_data_point).

{}

Returns:

Type Description

None

query_many_async(queries, num_connections=1, max_concurrent=100, client_kwargs={}, **query_kwargs) async

Execute many queries asynchronously using httpx.AsyncClient. Number of concurrent requests is controlled by the max_concurrent parameter. Each query will be retried up to 3 times using an exponential backoff strategy.

Parameters:

Name Type Description Default
queries Iterable[dict]

Iterable of query bodies (dictionaries) to be sent.

required
num_connections int

Number of connections to be used in the asynchronous client (uses HTTP/2). Defaults to 1.

1
max_concurrent int

Maximum concurrent requests to be sent. Defaults to 100. Be careful with increasing too much.

100
client_kwargs dict

Additional arguments to be passed to the httpx.AsyncClient.

{}
**query_kwargs dict

Additional arguments to be passed to the query method.

{}

Returns:

Type Description
List[VespaQueryResponse]

List[VespaQueryResponse]: List of VespaQueryResponse objects.

query_many(queries, num_connections=1, max_concurrent=100, client_kwargs={}, **query_kwargs)

Execute many queries asynchronously using httpx.AsyncClient. This method is a wrapper around the query_many_async method that uses the asyncio event loop to run the coroutine. Number of concurrent requests is controlled by the max_concurrent parameter. Each query will be retried up to 3 times using an exponential backoff strategy.

Parameters:

Name Type Description Default
queries Iterable[dict]

Iterable of query bodies (dictionaries) to be sent.

required
num_connections int

Number of connections to be used in the asynchronous client (uses HTTP/2). Defaults to 1.

1
max_concurrent int

Maximum concurrent requests to be sent. Defaults to 100. Be careful with increasing too much.

100
client_kwargs dict

Additional arguments to be passed to the httpx.AsyncClient.

{}
**query_kwargs dict

Additional arguments to be passed to the query method.

{}

Returns:

Type Description
List[VespaQueryResponse]

List[VespaQueryResponse]: List of VespaQueryResponse objects.

delete_data(schema, data_id, namespace=None, groupname=None, **kwargs)

Delete a data point from a Vespa app.

Example usage
app = Vespa(url="localhost", port=8080)
response = app.delete_data(schema="schema_name", data_id="1")
print(response)

Parameters:

Name Type Description Default
schema str

The schema that we are deleting data from.

required
data_id str

Unique id associated with this data point.

required
namespace str

The namespace that we are deleting data from. If no namespace is provided, the schema is used.

None
groupname str

The groupname that we are deleting data from.

None
**kwargs dict

Additional arguments to be passed to the HTTP DELETE request. See Vespa API documentation for more details.

{}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP DELETE request.

delete_all_docs(content_cluster_name, schema, namespace=None, slices=1, **kwargs)

Delete all documents associated with the schema. This might block for a long time as it requires sending multiple delete requests to complete.

Parameters:

Name Type Description Default
content_cluster_name str

Name of content cluster to GET from, or visit.

required
schema str

The schema that we are deleting data from.

required
namespace str

The namespace that we are deleting data from. If no namespace is provided, the schema is used.

None
slices int

Number of slices to use for parallel delete requests. Defaults to 1.

1
**kwargs dict

Additional arguments to be passed to the HTTP DELETE request. See Vespa API documentation for more details.

{}

Returns:

Name Type Description
Response Response

The response of the HTTP DELETE request.

visit(content_cluster_name, schema=None, namespace=None, slices=1, selection='true', wanted_document_count=500, slice_id=None, **kwargs)

Visit all documents associated with the schema and matching the selection.

Will run each slice on a separate thread, for each slice yields the response for each page.

Example usage
for slice in app.visit(schema="schema_name", slices=2):
    for response in slice:
        print(response.json)

Parameters:

Name Type Description Default
content_cluster_name str

Name of content cluster to GET from.

required
schema str

The schema that we are visiting data from.

None
namespace str

The namespace that we are visiting data from.

None
slices int

Number of slices to use for parallel GET.

1
selection str

Selection expression to filter documents.

'true'
wanted_document_count int

Best effort number of documents to retrieve for each request. May contain less if there are not enough documents left.

500
slice_id int

Slice id to use for the visit. If None, all slices will be used.

None
**kwargs dict

Additional HTTP request parameters. See Vespa API documentation.

{}

Yields:

Type Description
Generator[VespaVisitResponse, None, None]

Generator[Generator[Response]]: A generator of slices, each containing a generator of responses.

Raises:

Type Description
HTTPError

If an HTTP error occurred.

get_data(schema, data_id, namespace=None, groupname=None, raise_on_not_found=False, **kwargs)

Get a data point from a Vespa app.

Parameters:

Name Type Description Default
data_id str

Unique id associated with this data point.

required
schema str

The schema that we are getting data from. Will attempt to infer schema name if not provided.

required
namespace str

The namespace that we are getting data from. If no namespace is provided, the schema is used.

None
groupname str

The groupname that we are getting data from.

None
raise_on_not_found bool

Raise an exception if the data_id is not found. Default is False.

False
**kwargs dict

Additional arguments to be passed to the HTTP GET request. See Vespa API documentation.

{}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP GET request.

update_data(schema, data_id, fields, create=False, namespace=None, groupname=None, compress='auto', **kwargs)

Update a data point in a Vespa app.

Example usage
vespa = Vespa(url="localhost", port=8080)

fields = {"mystringfield": "value1", "myintfield": 42}
response = vespa.update_data(schema="schema_name", data_id="id1", fields=fields)
# or, with partial update, setting auto_assign=False
fields = {"myintfield": {"increment": 1}}
response = vespa.update_data(schema="schema_name", data_id="id1", fields=fields, auto_assign=False)
print(response.json)

Parameters:

Name Type Description Default
schema str

The schema that we are updating data.

required
data_id str

Unique id associated with this data point.

required
fields dict

Dict containing all the fields you want to update.

required
create bool

If true, updates to non-existent documents will create an empty document to update.

False
auto_assign bool

Assumes fields-parameter is an assignment operation. If set to false, the fields parameter should be a dictionary including the update operation.

required
namespace str

The namespace that we are updating data. If no namespace is provided, the schema is used.

None
groupname str

The groupname that we are updating data.

None
compress Union[str, bool]

Whether to compress the request body. Defaults to "auto", which will compress if the body is larger than 1024 bytes.

'auto'
**kwargs dict

Additional arguments to be passed to the HTTP PUT request. See Vespa API documentation.

{}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP PUT request.

get_model_from_application_package(model_name)

Get model definition from application package, if available.

predict(x, model_id, function_name='output_0')

Obtain a stateless model evaluation.

Parameters:

Name Type Description Default
x various

Input where the format depends on the task that the model is serving.

required
model_id str

The id of the model used to serve the prediction.

required
function_name str

The name of the output function to be evaluated.

'output_0'

Returns:

Name Type Description
var

Model prediction.

get_document_v1_path(id, schema=None, namespace=None, group=None, number=None)

Convert to document v1 path.

Parameters:

Name Type Description Default
id str

The id of the document.

required
namespace str

The namespace of the document.

None
schema str

The schema of the document.

None
group str

The group of the document.

None
number int

The number of the document.

None

Returns:

Name Type Description
str str

The path to the document v1 endpoint.

VespaSync(app, pool_maxsize=10, pool_connections=10, compress='auto')

Bases: object

Class to handle synchronous requests to Vespa. This class is intended to be used as a context manager.

Example usage
with VespaSync(app) as sync_app:
    response = sync_app.query(body=body)
print(response)

Can also be accessed directly through Vespa.syncio:

app = Vespa(url="localhost", port=8080)
with app.syncio() as sync_app:
    response = sync_app.query(body=body)
See also Vespa.feed_iterable for a convenient way to feed data synchronously.

Parameters:

Name Type Description Default
app Vespa

Vespa app object.

required
pool_maxsize int

The maximum number of connections to save in the pool. Defaults to 10.

10
pool_connections int

The number of urllib3 connection pools to cache. Defaults to 10.

10
compress Union[str, bool]

Whether to compress the request body. Defaults to "auto", which will compress if the body is larger than 1024 bytes.

'auto'

get_model_endpoint(model_id=None)

Get model evaluation endpoints.

predict(model_id, function_name, encoded_tokens)

Obtain a stateless model evaluation.

Parameters:

Name Type Description Default
model_id str

The id of the model used to serve the prediction.

required
function_name str

The name of the output function to be evaluated.

required
encoded_tokens str

URL-encoded input to the model.

required

Returns:

Type Description

The model prediction.

feed_data_point(schema, data_id, fields, namespace=None, groupname=None, **kwargs)

Feed a data point to a Vespa app.

Parameters:

Name Type Description Default
schema str

The schema that we are sending data to.

required
data_id str

Unique id associated with this data point.

required
fields dict

Dict containing all the fields required by the schema.

required
namespace str

The namespace that we are sending data to. If no namespace is provided, the schema is used.

None
groupname str

The group that we are sending data to.

None
**kwargs dict {}

Returns:

Type Description
VespaResponse

The response of the HTTP POST request.

Raises:

Type Description
HTTPError

If one occurred.

query(body=None, groupname=None, **kwargs)

Send a query request to the Vespa application.

Parameters:

Name Type Description Default
body dict

Dict containing all the request parameters.

None
groupname str

The groupname used in streaming search.

None
**kwargs dict

Additional valid Vespa HTTP Query API parameters. See: https://docs.vespa.ai/en/reference/query-api-reference.html.

{}

Returns:

Type Description
VespaQueryResponse

The request body if debug_request is True, or the result from the Vespa application.

Raises:

Type Description
HTTPError

If one occurred.

delete_data(schema, data_id, namespace=None, groupname=None, **kwargs)

Delete a data point from a Vespa app.

Parameters:

Name Type Description Default
schema str

The schema that we are deleting data from.

required
data_id str

Unique id associated with this data point.

required
namespace str

The namespace that we are deleting data from.

None
**kwargs dict {}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP DELETE request.

Raises:

Type Description
HTTPError

If one occurred.

delete_all_docs(content_cluster_name, schema, namespace=None, slices=1, **kwargs)

Delete all documents associated with the schema.

Parameters:

Name Type Description Default
content_cluster_name str

Name of content cluster to GET from or visit.

required
schema str

The schema that we are deleting data from.

required
namespace str

The namespace that we are deleting data from.

None
slices int

Number of slices to use for parallel delete.

1
**kwargs dict {}

Returns:

Name Type Description
Response None

The response of the HTTP DELETE request.

Raises:

Type Description
HTTPError

If one occurred.

visit(content_cluster_name, schema=None, namespace=None, slices=1, selection='true', wanted_document_count=500, slice_id=None, **kwargs)

Visit all documents associated with the schema and matching the selection.

This method will run each slice on a separate thread, yielding the response for each page for each slice.

Parameters:

Name Type Description Default
content_cluster_name str

Name of content cluster to GET from.

required
schema str

The schema that we are visiting data from.

None
namespace str

The namespace that we are visiting data from.

None
slices int

Number of slices to use for parallel GET.

1
wanted_document_count int

Best effort number of documents to retrieve for each request. May contain fewer if there are not enough documents left.

500
selection str

Selection expression to use. Defaults to "true". See: https://docs.vespa.ai/en/reference/document-select-language.html.

'true'
slice_id int

Slice id to use. Defaults to -1, which means all slices.

None
**kwargs dict {}

Returns:

Name Type Description
generator None

A generator of slices, each containing a generator of responses.

Raises:

Type Description
HTTPError

If one occurred.

get_data(schema, data_id, namespace=None, groupname=None, raise_on_not_found=False, **kwargs)

Get a data point from a Vespa app.

Parameters:

Name Type Description Default
schema str

The schema that we are getting data from.

required
data_id str

Unique id associated with this data point.

required
namespace str

The namespace that we are getting data from.

None
groupname str

The groupname used to get data.

None
raise_on_not_found bool

Raise an exception if the document is not found. Default is False.

False
**kwargs dict {}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP GET request.

Raises:

Type Description
HTTPError

If one occurred.

update_data(schema, data_id, fields, create=False, auto_assign=True, namespace=None, groupname=None, **kwargs)

Update a data point in a Vespa app.

Parameters:

Name Type Description Default
schema str

The schema that we are updating data in.

required
data_id str

Unique id associated with this data point.

required
fields dict

Dict containing all the fields you want to update.

required
create bool

If true, updates to non-existent documents will create an empty document to update. Default is False.

False
auto_assign bool

Assumes fields-parameter is an assignment operation. If set to False, the fields parameter should include the update operation. Default is True.

True
namespace str

The namespace that we are updating data in.

None
groupname str

The groupname used to update data.

None
**kwargs dict

Additional HTTP request parameters. See: <https://docs.vespa.ai/en/reference/document-v1-api-reference.html#request-parameters.

{}

Returns:

Name Type Description
Response VespaResponse

The response of the HTTP PUT request.

Raises:

Type Description
HTTPError

If one occurred.

VespaAsync(app, connections=1, total_timeout=None, timeout=httpx.Timeout(5), **kwargs)

Bases: object

Class to handle asynchronous HTTP connections to Vespa.

Uses httpx as the async HTTP client, and HTTP/2 by default. This class is intended to be used as a context manager.

Basic usage:

async with VespaAsync(app) as async_app:
    response = await async_app.query(
        body={"yql": "select * from sources * where title contains 'music';"}
    )

Passing custom timeout and limits:

import httpx

timeout = httpx.Timeout(10.0, connect=5.0)
limits = httpx.Limits(max_connections=10, max_keepalive_connections=5)

async with VespaAsync(app, timeout=timeout, limits=limits) as async_app:
    response = await async_app.query(
        body={"yql": "select * from sources * where title contains 'music';"}
    )

Using additional kwargs (e.g., proxies):

proxies = "http://localhost:8080"

async with VespaAsync(app, proxies=proxies) as async_app:
    response = await async_app.query(
        body={"yql": "select * from sources * where title contains 'music';"}
    )

Accessing via Vespa.asyncio:

app = Vespa(url="localhost", port=8080)
async with app.asyncio(timeout=timeout, limits=limits) as async_app:
    response = await async_app.query(
        body={"yql": "select * from sources * where title contains 'music';"}
    )

See also Vespa.feed_async_iterable for a convenient interface to async data feeding.

Parameters:

Name Type Description Default
app Vespa

Vespa application object.

required
connections Optional[int]

Number of connections. Defaults to 1 as HTTP/2 is multiplexed.

1
total_timeout int

Deprecated. Will be ignored and removed in future versions. Use timeout to pass an httpx.Timeout object instead.

None
timeout Timeout

Timeout settings for the httpx.AsyncClient. Defaults to httpx.Timeout(5).

Timeout(5)
**kwargs

Additional arguments to be passed to the httpx.AsyncClient. See HTTPX AsyncClient documentation for more details.

{}
Note
  • Passing timeout allows you to configure timeouts for connect, read, write, and overall request time.
  • The limits parameter can be used to control connection pooling behavior, such as the maximum number of concurrent connections.
  • See HTTPX documentation for more information on httpx and its features.

raise_for_status(response, raise_on_not_found=False)

Raises an appropriate error if necessary.

If the response contains an error message, VespaError is raised along with HTTPError to provide more details.

Parameters:

Name Type Description Default
response Response

Response object from the Vespa API.

required
raise_on_not_found bool

If True, raises HTTPError if status_code is 404.

False

Raises:

Type Description
HTTPError

If status_code is between 400 and 599.

VespaError

If the response JSON contains an error message.