Agent Data Handling
How to handle data formats in your agents
We provide a few different ways to handle data formats in your agents to make it easier to work with different data types. Of course, your agent can always perform its own data handling by use the raw data and the content type property. However, most common data types are supported out of the box.
How it works
Data that is sent to your agent is transferred as raw binary data and the content type is provided to the agent. The agent can then use the content type to determine how to handle the data. The AgentRequest
object provides a data
property that contains the Data
object. The Data
object provides a contentType
property that contains the content type of the data. The Data
object also provides a number of helper methods to help you handle the data.
When you send data to your agent using the HTTP protocol, for example, the Content-Type
HTTP header is used to determine the content type of the data. If you are sending binary type data, make sure that you use raw binary data and set the Content-Type
HTTP header to the appropriate value. When using Agent-to-Agent communication, the Agentuity SDK will automatically determine the content type of the data based on the data that is sent. You can override the content type by passing the content type.
Often, your agent will need to handle different formats dynamically. In this case, you can use the contentType
property to determine the format of the data.
Request Data Formats
The following request data formats are supported out of the box:
Text
You use use the text
method on the Data
object to get the raw text data.
You must await the text
method to get the raw text data since the data could be a stream. In the case the data is streamed to the agent, the agent will wait for the data to be fully streamed before returning the text data.
In the case the data is another content type that isn't text, the text
method will return the raw data as a string. For example, if the content type is application/json
, the text
method will return the raw JSON data as a string.
JSON
You can use the json
method on the Data
object to get the raw JSON data.
You must await the json
method to get the raw JSON data since the data could be a stream. In the case the data is streamed to the agent, the agent will wait for the data to be fully streamed before returning the JSON data.
In the case the data is another content type that isn't JSON, the json
method will attempt to parse the data as JSON. If the data is not valid JSON, the json
method will throw an error.
Object
You can use the object
method on the Data
object to get the JSON data as an object cast to the type you provide. This currently only works for JSON data and the JavaScript SDK.
Binary
If you want to get the raw binary data, you can use the binary
method on the Data
object.
You must await the binary
method to get the raw binary data since the data could be a stream. In the case the data is streamed to the agent, the agent will wait for the data to be fully streamed before returning the binary data.
For JavaScript, the binary
method returns a Uint8Array
object. For Python, the binary
method returns a bytes
object.
Stream
If you want to get the raw binary data as a stream, you can use the stream
method on the Data
object.
You must await the stream
method to get a stream of the raw binary data. The stream will be a ReadableStream
object for JavaScript and a IO[bytes]
object for Python.
See the Streaming guide for more information on how Agent Streaming works.
Base64
If you want to get the raw binary data as a base64 encoded string, you can use the base64
method on the Data
object.
You must await the base64
method to get the base64 encoded string.
If you want to get the raw binary data as an email object, you can use the email
method on the Data
object. This assumes that the request payload was an RFC822 encoded email.
The email object is only supported when you setup your agent to receive emails.
You must await the email
method to get the email object. The email object will be a Email
object. The Email object has the following properties:
date
: The date of the email.messageId
: The message ID of the email.fromEmail
: The sender of the email.fromName
: The sender name (if provided).subject
: The subject of the email.attachments
: The attachments of the email as an array ofAttachment
objects.headers
: The headers of the email as an array ofHeader
objects.text
: The text body of the email.html
: The HTML body of the email.
The Attachment
object has the following properties:
filename
: The filename of the attachment.contentDisposition
: The content disposition of the attachment which is eitherinline
orattachment
. Defaults toattachment
.data
: TheDataType
of the attachment.
Response Data Formats
The following response data formats are supported out of the box. Use these methods on the AgentResponse
object to send data in the desired format from your agent.
Text
You can use the text
method to send a plain text response.
JSON
You can use the json
method to send a JSON response.
Binary
You can use the binary
method to send raw binary data.
Media Types (Images, Audio, Video, PDF, etc.)
The SDK provides helpers for common media types. Use the corresponding method for the type you want to return (e.g., png
, jpeg
, pdf
, mp3
, etc.).
Markdown
You can use the markdown
method to return markdown content.
HTML
You can use the html
method to return HTML content.
Streaming
For large or real-time responses, you can stream data using the stream
method. The source can be an async iterator, a stream, or another agent's stream.
Custom Response
For advanced use cases, you can return a native Response object from your agent.
For JavaScript, the Response
object follows the Fetch API interface.
For Python, the Response
object follows the aiohttp.ClientResponse interface.
Need Help?
Join our Community for assistance or just to hang with other humans building agents.
Send us an email at hi@agentuity.com if you'd like to get in touch.
Please Follow us on
If you haven't already, please Signup for your free account now and start building your first agent!