Skip to main content
Version: Beta 🚧

Read Online Features for Inference using the Python Client

Tecton provides an open-source client library in Python for reading features from the online store. The Python Client API Reference can be found here.

Installing the Python Client library​

The latest version of the Tecton Python Client can be found on PyPi.

For any project, install the client library using pip:

pip install tecton-client

Creating an API key to authenticate to the HTTP API​

To authenticate your requests to the HTTP API, you will need to create a Service Account to obtain an API key, and grant that Service Account the Consumer role for your workspace:

  1. Create a Service Account to obtain your API key.
tecton service-account create \
--name "sample-service-account" \
--description "An online inference sample"

Output:

Save this API Key - you will not be able to get it again.
API Key: <Your-api-key>
Service Account ID: <Your-Service-Account-Id>
  1. Assign the Consumer role to the Service Account.
tecton access-control assign-role --role consumer \
--workspace <Your-workspace> \
--service-account <Your-Service-Account-Id>

Output:

Successfully updated role.

Save this API key for the next step.

TectonClient Example​

To call the get-features endpoint, first create a client using the base URL for your cluster (https://<your Tecton instance prefix>.tecton.ai) and the API key from the step above. Then call get_features(). Below is an example calling the fraud_detection_feature_service:v2 service in https://explore.tecton.ai/:

from tecton_client import TectonClient

tecton_client = TectonClient(url="https://explore.tecton.ai/", api_key="your-api-key", default_workspace_name="prod")

resp = tecton_client.get_features(
feature_service_name="fraud_detection_feature_service:v2",
join_key_map={"user_id": "user_4407104885"},
request_context_map={"amount": 500.00},
)

print(resp.get_features_dict())

You can add additional metadata for debugging such as the the server response time using the metadata_options argument:

from tecton_client import MetadataOptions

resp = tecton_client.get_features(
feature_service_name="fraud_detection_feature_service:v2",
join_key_map={"user_id": "user_4407104885"},
request_context_map={"amount": 500.00},
metadata_options=MetadataOptions(include_slo_info=True),
)

print(resp.metadata)

AsyncTectonClient Example​

The AsyncTectonClient has the same method signatures as the (synchronous) TectonClient, but is designed to leverage Python’s async/await syntax for non-blocking I/O operations. To use the asynchronous features, you will need some familiarity with Python’s async/await syntax. Replace your synchronous client calls with the async counterparts provided by Tecton, and manage them within an async function or an event loop.

import asyncio
from tecton_client import AsyncTectonClient

async_client = AsyncTectonClient(
url="https://explore.tecton.ai/",
api_key="your-api-key",
default_workspace_name="prod",
)


async def fetch_data():
resp = async_client.get_features(
feature_service_name="fraud_detection_feature_service:v2",
join_key_map={"user_id": "user_4407104885"},
request_context_map={"amount": 500.00},
)
print(resp.result.features)


asyncio.run(fetch_data())

For more information on using the client library, refer to the official documentation.

Was this page helpful?