Python API Reference (SaaS)
This section contains the API reference for the LanceDB Cloud Python API.
Installation
Connection
lancedb.connect(uri: URI, *, api_key: Optional[str] = None, region: str = 'us-east-1', host_override: Optional[str] = None, read_consistency_interval: Optional[timedelta] = None, request_thread_pool: Optional[Union[int, ThreadPoolExecutor]] = None, client_config: Union[ClientConfig, Dict[str, Any], None] = None, **kwargs: Any) -> DBConnection
Connect to a LanceDB database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri |
URI
|
The uri of the database. |
required |
api_key |
Optional[str]
|
If presented, connect to LanceDB cloud.
Otherwise, connect to a database on file system or cloud storage.
Can be set via environment variable |
None
|
region |
str
|
The region to use for LanceDB Cloud. |
'us-east-1'
|
host_override |
Optional[str]
|
The override url for LanceDB Cloud. |
None
|
read_consistency_interval |
Optional[timedelta]
|
(For LanceDB OSS only) The interval at which to check for updates to the table from other processes. If None, then consistency is not checked. For performance reasons, this is the default. For strong consistency, set this to zero seconds. Then every read will check for updates from other processes. As a compromise, you can set this to a non-zero timedelta for eventual consistency. If more than that interval has passed since the last check, then the table will be checked for updates. Note: this consistency only applies to read operations. Write operations are always consistent. |
None
|
client_config |
Union[ClientConfig, Dict[str, Any], None]
|
Configuration options for the LanceDB Cloud HTTP client. If a dict, then the keys are the attributes of the ClientConfig class. If None, then the default configuration is used. |
None
|
Examples:
For a local directory, provide a path for the database:
For object storage, use a URI prefix:
Connect to LanceDB cloud:
Returns:
Name | Type | Description |
---|---|---|
conn |
DBConnection
|
A connection to a LanceDB database. |
Source code in lancedb/__init__.py
lancedb.remote.db.RemoteDBConnection
Bases: DBConnection
A connection to a remote LanceDB database.
Source code in lancedb/remote/db.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
__init__(db_url: str, api_key: str, region: str, host_override: Optional[str] = None, request_thread_pool: Optional[ThreadPoolExecutor] = None, client_config: Union[ClientConfig, Dict[str, Any], None] = None, connection_timeout: Optional[float] = None, read_timeout: Optional[float] = None)
Connect to a remote LanceDB database.
Source code in lancedb/remote/db.py
table_names(page_token: Optional[str] = None, limit: int = 10) -> Iterable[str]
List the names of all tables in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page_token |
Optional[str]
|
The last token to start the new page. |
None
|
limit |
int
|
The maximum number of tables to return for each page. |
10
|
Returns:
Type | Description |
---|---|
An iterator of table names.
|
|
Source code in lancedb/remote/db.py
open_table(name: str, *, index_cache_size: Optional[int] = None) -> Table
Open a Lance Table in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the table. |
required |
Returns:
Type | Description |
---|---|
A LanceTable object representing the table.
|
|
Source code in lancedb/remote/db.py
create_table(name: str, data: DATA = None, schema: Optional[Union[pa.Schema, LanceModel]] = None, on_bad_vectors: str = 'error', fill_value: float = 0.0, mode: Optional[str] = None, embedding_functions: Optional[List[EmbeddingFunctionConfig]] = None) -> Table
Create a Table in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the table. |
required |
data |
DATA
|
User must provide at least one of
|
None
|
schema |
Optional[Union[Schema, LanceModel]]
|
Acceptable types are:
|
None
|
on_bad_vectors |
str
|
What to do if any of the vectors are not the same size or contains NaNs. One of "error", "drop", "fill". |
'error'
|
fill_value |
float
|
The value to use when filling vectors. Only used if on_bad_vectors="fill". |
0.0
|
Returns:
Type | Description |
---|---|
LanceTable
|
A reference to the newly created table. |
!!! note
|
The vector index won't be created by default.
To create the index, call the |
Examples:
Can create with list of tuples or dictionaries:
>>> import lancedb
>>> db = lancedb.connect("db://...", api_key="...",
... region="...")
>>> data = [{"vector": [1.1, 1.2], "lat": 45.5, "long": -122.7},
... {"vector": [0.2, 1.8], "lat": 40.1, "long": -74.1}]
>>> db.create_table("my_table", data)
LanceTable(my_table)
You can also pass a pandas DataFrame:
>>> import pandas as pd
>>> data = pd.DataFrame({
... "vector": [[1.1, 1.2], [0.2, 1.8]],
... "lat": [45.5, 40.1],
... "long": [-122.7, -74.1]
... })
>>> db.create_table("table2", data)
LanceTable(table2)
>>> custom_schema = pa.schema([
... pa.field("vector", pa.list_(pa.float32(), 2)),
... pa.field("lat", pa.float32()),
... pa.field("long", pa.float32())
... ])
>>> db.create_table("table3", data, schema = custom_schema)
LanceTable(table3)
It is also possible to create an table from [Iterable[pa.RecordBatch]]
:
>>> import pyarrow as pa
>>> def make_batches():
... for i in range(5):
... yield pa.RecordBatch.from_arrays(
... [
... pa.array([[3.1, 4.1], [5.9, 26.5]],
... pa.list_(pa.float32(), 2)),
... pa.array(["foo", "bar"]),
... pa.array([10.0, 20.0]),
... ],
... ["vector", "item", "price"],
... )
>>> schema=pa.schema([
... pa.field("vector", pa.list_(pa.float32(), 2)),
... pa.field("item", pa.utf8()),
... pa.field("price", pa.float32()),
... ])
>>> db.create_table("table4", make_batches(), schema=schema)
LanceTable(table4)
Source code in lancedb/remote/db.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
drop_table(name: str)
Drop a table from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the table. |
required |
rename_table(cur_name: str, new_name: str)
Rename a table in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cur_name |
str
|
The current name of the table. |
required |
new_name |
str
|
The new name of the table. |
required |
Source code in lancedb/remote/db.py
Table
lancedb.remote.table.RemoteTable
Bases: Table
Source code in lancedb/remote/table.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
|
name: str
property
The name of the table
schema: pa.Schema
property
The Arrow Schema of this Table
version: int
property
Get the current version of the table
embedding_functions: dict
cached
property
Get the embedding functions for the table
Returns:
Name | Type | Description |
---|---|---|
funcs |
dict
|
A mapping of the vector column to the embedding function or empty dict if not configured. |
to_arrow() -> pa.Table
to_pandas()
list_indices()
index_stats(index_uuid: str)
create_scalar_index(column: str, index_type: Literal['BTREE', 'BITMAP', 'LABEL_LIST', 'scalar'] = 'scalar', *, replace: bool = False)
Creates a scalar index
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
str
|
The column to be indexed. Must be a boolean, integer, float, or string column. |
required |
index_type |
str
|
The index type of the scalar index. Must be "scalar" (BTREE), "BTREE", "BITMAP", or "LABEL_LIST", |
'scalar'
|
replace |
bool
|
If True, replace the existing index with the new one. |
False
|
Source code in lancedb/remote/table.py
create_index(metric='L2', vector_column_name: str = VECTOR_COLUMN_NAME, index_cache_size: Optional[int] = None, num_partitions: Optional[int] = None, num_sub_vectors: Optional[int] = None, replace: Optional[bool] = None, accelerator: Optional[str] = None, index_type='vector')
Create an index on the table. Currently, the only parameters that matter are the metric and the vector column name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
str
|
The metric to use for the index. Default is "L2". |
'L2'
|
vector_column_name |
str
|
The name of the vector column. Default is "vector". |
VECTOR_COLUMN_NAME
|
Examples:
>>> import lancedb
>>> import uuid
>>> from lancedb.schema import vector
>>> db = lancedb.connect("db://...", api_key="...",
... region="...")
>>> table_name = uuid.uuid4().hex
>>> schema = pa.schema(
... [
... pa.field("id", pa.uint32(), False),
... pa.field("vector", vector(128), False),
... pa.field("s", pa.string(), False),
... ]
... )
>>> table = db.create_table(
... table_name,
... schema=schema,
... )
>>> table.create_index("L2", "vector")
Source code in lancedb/remote/table.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
add(data: DATA, mode: str = 'append', on_bad_vectors: str = 'error', fill_value: float = 0.0) -> int
Add more data to the Table. It has the same API signature as the OSS version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DATA
|
The data to insert into the table. Acceptable types are:
|
required |
mode |
str
|
The mode to use when writing the data. Valid values are "append" and "overwrite". |
'append'
|
on_bad_vectors |
str
|
What to do if any of the vectors are not the same size or contains NaNs. One of "error", "drop", "fill". |
'error'
|
fill_value |
float
|
The value to use when filling vectors. Only used if on_bad_vectors="fill". |
0.0
|
Source code in lancedb/remote/table.py
search(query: Union[VEC, str] = None, vector_column_name: Optional[str] = None, query_type='auto', fts_columns: Optional[Union[str, List[str]]] = None, fast_search: bool = False) -> LanceVectorQueryBuilder
Create a search query to find the nearest neighbors of the given query vector. We currently support vector search
All query options are defined in Query.
Examples:
>>> import lancedb
>>> db = lancedb.connect("db://...", api_key="...",
... region="...")
>>> data = [
... {"original_width": 100, "caption": "bar", "vector": [0.1, 2.3, 4.5]},
... {"original_width": 2000, "caption": "foo", "vector": [0.5, 3.4, 1.3]},
... {"original_width": 3000, "caption": "test", "vector": [0.3, 6.2, 2.6]}
... ]
>>> table = db.create_table("my_table", data)
>>> query = [0.4, 1.4, 2.4]
>>> (table.search(query)
... .where("original_width > 1000", prefilter=True)
... .select(["caption", "original_width"])
... .limit(2)
... .to_pandas())
caption original_width vector _distance
0 foo 2000 [0.5, 3.4, 1.3] 5.220000
1 test 3000 [0.3, 6.2, 2.6] 23.089996
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[VEC, str]
|
The targetted vector to search for.
|
None
|
vector_column_name |
Optional[str]
|
The name of the vector column to search.
|
None
|
fast_search |
bool
|
Skip a flat search of unindexed data. This may improve search performance but search results will not include unindexed data.
|
False
|
Returns:
Type | Description |
---|---|
LanceQueryBuilder
|
A query builder object representing the query. Once executed, the query returns
|
Source code in lancedb/remote/table.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
merge_insert(on: Union[str, Iterable[str]]) -> LanceMergeInsertBuilder
Returns a LanceMergeInsertBuilder
that can be used to create a "merge insert" operation.
See Table.merge_insert
for more details.
Source code in lancedb/remote/table.py
delete(predicate: str)
Delete rows from the table.
This can be used to delete a single row, many rows, all rows, or sometimes no rows (if your predicate matches nothing).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicate |
str
|
The SQL where clause to use when deleting rows.
The filter must not be empty, or it will error. |
required |
Examples:
>>> import lancedb
>>> data = [
... {"x": 1, "vector": [1, 2]},
... {"x": 2, "vector": [3, 4]},
... {"x": 3, "vector": [5, 6]}
... ]
>>> db = lancedb.connect("db://...", api_key="...",
... region="...")
>>> table = db.create_table("my_table", data)
>>> table.search([10,10]).to_pandas()
x vector _distance
0 3 [5.0, 6.0] 41.0
1 2 [3.0, 4.0] 85.0
2 1 [1.0, 2.0] 145.0
>>> table.delete("x = 2")
>>> table.search([10,10]).to_pandas()
x vector _distance
0 3 [5.0, 6.0] 41.0
1 1 [1.0, 2.0] 145.0
If you have a list of values to delete, you can combine them into a
stringified list and use the IN
operator:
>>> to_remove = [1, 3]
>>> to_remove = ", ".join([str(v) for v in to_remove])
>>> table.delete(f"x IN ({to_remove})")
>>> table.search([10,10]).to_pandas()
x vector _distance
0 2 [3.0, 4.0] 85.0
Source code in lancedb/remote/table.py
update(where: Optional[str] = None, values: Optional[dict] = None, *, values_sql: Optional[Dict[str, str]] = None)
This can be used to update zero to all rows depending on how many rows match the where clause.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
where |
Optional[str]
|
The SQL where clause to use when updating rows. For example, 'x = 2' or 'x IN (1, 2, 3)'. The filter must not be empty, or it will error. |
None
|
values |
Optional[dict]
|
The values to update. The keys are the column names and the values are the values to set. |
None
|
values_sql |
Optional[Dict[str, str]]
|
The values to update, expressed as SQL expression strings. These can reference existing columns. For example, {"x": "x + 1"} will increment the x column by 1. |
None
|
Examples:
>>> import lancedb
>>> data = [
... {"x": 1, "vector": [1, 2]},
... {"x": 2, "vector": [3, 4]},
... {"x": 3, "vector": [5, 6]}
... ]
>>> db = lancedb.connect("db://...", api_key="...",
... region="...")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
>>> table.update(where="x = 2", values={"vector": [10, 10]})
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 3 [5.0, 6.0]
2 2 [10.0, 10.0]
Source code in lancedb/remote/table.py
cleanup_old_versions(*_)
cleanup_old_versions() is not supported on the LanceDB cloud
compact_files(*_)
optimize(*, cleanup_older_than: Optional[timedelta] = None, delete_unverified: bool = False)
optimize() is not supported on the LanceDB cloud. Indices are optimized automatically.