API Reference
Complete reference documentation for the Agentuity Python SDK, including request handling, response types, data storage, and agent communication.
This section provides detailed documentation for the Agentuity Python SDK API, including method signatures, parameters, return values, and example usage.
Table of Contents
Note: This documentation applies to Agentuity Python SDK version 1.0 and above.
Agent Lifecycle
The Agentuity SDK provides a structured way to define and handle agents. An agent consists of a handler function, with its configuration managed by the Agentuity CLI.
Agent Handler
The agent handler is a function that processes requests and returns responses:
Parameters
request
: AnAgentRequest
object containing the request dataresponse
: AnAgentResponse
object for creating responsescontext
: AnAgentContext
object providing access to various capabilities
Return Value
The handler function should return an AgentResponseType
object.
Example
Storage APIs
The Agentuity SDK provides two storage APIs: Key-Value Storage and Vector Storage.
Key-Value Storage
The Key-Value Storage API provides a simple way to store and retrieve data. It is accessed through the context.kv
object.
async get(name: str, key: str) -> "DataResult"
Retrieves a value from the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to retrieve the value for
Return Value
Returns a DataResult object that has an exists
property to check if the value exists and a data
property with accessors like data.json
and data.text
.
Example
async set(name: str, key: str, value: Union[str, int, float, bool, list, dict, bytes, "Data"], params: Optional[dict] = None) -> None
Stores a value in the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to store the value undervalue
: The value to store (Union[str, int, float, bool, list, dict, bytes, "Data"])params
(optional): Can havettl
(time-to-live in seconds)
Return Value
None
Example
async delete(name: str, key: str) -> None
Deletes a value from the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to delete
Return Value
None
Example
Vector Storage
The Vector Storage API provides a way to store and search for data using vector embeddings. It is accessed through the context.vector
object.
async upsert(name: str, *documents: VectorUpsertParams) -> list[str]
Inserts or updates vectors in the vector storage.
Parameters
name
: The name of the vector storagedocuments
: One or more documents to upsert, each with either embeddings or text
Return Value
Returns a list of string IDs for the upserted vectors.
Example
async search(name: str, params: VectorSearchParams) -> list[VectorSearchResult]
Searches for vectors in the vector storage.
Parameters
name
: The name of the vector storageparams
: Search parameters including query, limit, similarity threshold, and metadata filters
Return Value
Returns a list of search results, each containing an ID, metadata, and distance score.
Example
async delete(name: str, *ids: str) -> int
Deletes vectors from the vector storage.
Parameters
name
: The name of the vector storageids
: One or more IDs of vectors to delete
Return Value
Returns the number of vectors that were deleted.
Example
Agent Communication
The Agentuity SDK allows agents to communicate with each other through the context.get_agent()
method and agent redirection.
async get_agent(params: GetAgentRequestParams) -> RemoteAgent
Retrieves a handle to a remote agent that can be invoked.
Parameters
params
: Parameters to identify the agent, either by ID or by name and project ID
Return Value
Returns a RemoteAgent
object that can be used to invoke the agent.
Example
Agent Handoff
The response.handoff()
method allows an agent to handoff the request to another agent.
handoff(agent: GetAgentRequestParams, payload: dict | bytes | str = None, content_type: str = None, metadata: dict = None) -> AgentRedirectResponse
Redirects the current request to another agent.
Handoff
Handoff is currently only supported for handoff to other agents in the same project. However, we are working on remote agent handoff and should have that working soon.
Parameters
agent
: Parameters to identify the target agentpayload
(optional): The payload to send to the target agentcontent_type
(optional): The content type of the payloadmetadata
(optional): Additional metadata to include with the request
Return Value
Returns an AgentRedirectResponse
object.
Example
Response Types
The Agentuity SDK provides various methods for creating different types of responses through the response
object.
JSON Responses
json(data: dict, metadata: dict = None) -> AgentResponseType
Creates a JSON response.
Parameters
data
: The JSON data to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the JSON data.
Example
Text Responses
text(data: str, metadata: dict = None) -> AgentResponseType
Creates a text response.
Parameters
data
: The text to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the text data.
Example
Binary Responses
binary(data: bytes, metadata: dict = None) -> AgentResponseType
Creates a binary response.
Parameters
data
: The binary data to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the binary data.
Example
Media Type Responses
The SDK provides specialized methods for various media types:
pdf(data: bytes, metadata: dict = None) -> AgentResponseType
png(data: bytes, metadata: dict = None) -> AgentResponseType
jpeg(data: bytes, metadata: dict = None) -> AgentResponseType
gif(data: bytes, metadata: dict = None) -> AgentResponseType
webp(data: bytes, metadata: dict = None) -> AgentResponseType
mp3(data: bytes, metadata: dict = None) -> AgentResponseType
mp4(data: bytes, metadata: dict = None) -> AgentResponseType
wav(data: bytes, metadata: dict = None) -> AgentResponseType
ogg(data: bytes, metadata: dict = None) -> AgentResponseType
Each method works similarly to the binary()
method but sets the appropriate content type.
Example
HTML Responses
html(data: str, metadata: dict = None) -> AgentResponseType
Creates an HTML response.
Parameters
data
: The HTML content to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the HTML content.
Example
Empty Responses
empty(metadata: dict = None) -> AgentResponseType
Creates an empty response.
Parameters
metadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with no payload.
Example
Request Handling
The Agentuity SDK provides various methods for accessing request data through the request
object.
Accessing Request Data
trigger() -> str
Gets the trigger type of the request.
Return Value
Returns a string representing the trigger type (webhook, cron, manual, agent, etc.).
Example
metadata(key: str, default_value = None) -> Any
Gets metadata associated with the request.
Parameters
key
: The metadata key to retrievedefault_value
(optional): A default value to return if the key does not exist
Return Value
Returns the metadata value for the specified key, or the default value if the key does not exist.
Example
json() -> dict
Gets the payload of the request as a JSON object.
Return Value
Returns the request payload as a dictionary.
Example
text() -> str
Gets the payload of the request as a string.
Return Value
Returns the request payload as a string.
Example
binary() -> bytes
Gets the payload of the request as bytes.
Return Value
Returns the request payload as bytes.
Example
Media-Specific Methods
The SDK provides specialized methods for various media types:
pdf() -> bytes
png() -> bytes
jpeg() -> bytes
gif() -> bytes
webp() -> bytes
mp3() -> bytes
wav() -> bytes
ogg() -> bytes
Each method returns the request payload as bytes with the appropriate content type validation.
Example
Logging
The Agentuity SDK provides logging functionality through the context.logger
object.
Logger Interface
The Logger
class defines the following methods:
Logging Methods
debug(message: str, *args, **kwargs) -> None
Logs a debug message.
Parameters
message
: The message to logargs
,kwargs
: Additional arguments to include in the log
Example
info(message: str, *args, **kwargs) -> None
Logs an informational message.
Parameters
message
: The message to logargs
,kwargs
: Additional arguments to include in the log
Example
warn(message: str, *args, **kwargs) -> None
Logs a warning message.
Parameters
message
: The message to logargs
,kwargs
: Additional arguments to include in the log
Example
error(message: str, *args, **kwargs) -> None
Logs an error message.
Parameters
message
: The message to logargs
,kwargs
: Additional arguments to include in the log
Example
Creating Child Loggers
child(**kwargs) -> Logger
Creates a child logger with additional context.
Parameters
kwargs
: Additional context to include in all logs from the child logger
Return Value
Returns a new Logger
instance with the additional context.
Example
Telemetry
The Agentuity SDK integrates with OpenTelemetry for tracing and metrics.
Tracing
The SDK provides access to OpenTelemetry tracing through the context.tracer
object.