Extract Structured Data
You can use agents to extract structured JSON objects from any data (text, audio, or images).
The examples shown in this page works just the same on both the server-side and client-side
Usage
All you need to do is define a zod schema of the data structure you want to get back, the zod schema is then converted into a JSON schema and passed to the agent, it’s also used to validate the agent’s output… yes all agents outputs are not only typed, but also validated ensuring reliability.
Example usage:
the output of the above code would be this:
Validation
The data you get back is validated against the schema you provide, for now if the validation fails an error will be thrown, and we’re working on better error handling for this case.
Context-aware extraction
This is a really fun part, let’s say that the user has an ongoing coversation with your agent in the session session_1233
,
and you want to generate some JSON data to do some smart behavior or analytics in your app, well, it’s as simple as adding one line of code:
yes, you just add the session ID and the output will ne generative based on the conversation context.
For example you can extract a list of suggested messages that the user could ask next (suggestions) based on the ongoing coversation:
Full docs
Refer to structuredOutput documentation for full docs.
Limitations
Most LLMs can’t perform well on complex data structures and nested objects, so it’s recommended to keep your schema as simple and clear as possible.
Was this page helpful?