Structured Output
Last updated
Last updated
In numerous use cases, such as chatbots, models are expected to reply to users in natural language. However, there are situations where natural language responses aren’t ideal. For instance, if we need to take the model’s output, pass it as a body for HTTP request, or store into a database, it's essential that the output aligns with a predefined schema. This requirement gives rise to the concept of structured output, where models are guided to generate responses in a specific, structured format.
In this tutorial we are going to take a look at how to generate a structured output from LLM, and pass it as the body for HTTP request.
We are going to use the same for HTTP request.
Absolutely! Here’s a tutorial for your Structured Output Flow in a format consistent with your "Agent as Tool" documentation, including step-by-step explanations and image placeholders.
Receives user input through a Start node.
Uses an LLM to generate a structured JSON array.
Loops through each item in the array.
Sends each item via HTTP to an external endpoint.
Begin by adding a Start node to your canvas.
Key Input Parameters:
Input Type:
chatInput
(default): The flow starts with a chat message from the user.
formInput
: The flow starts with a form (if you want to collect structured data from the user).
Ephemeral Memory:
(Optional) If enabled, the flow does not retain chat history between runs.
Flow State:
(Optional) Pre-populate state variables.
Example:
Persist State:
(Optional) If enabled, the state is persisted across the same session.
Add a LLM node and connect it to the Start node.
Purpose: Uses a language model to analyze the input and generate a structured JSON array.
Key Input Parameters:
JSON Structured Output:
Key: answers
Type: JSON Array
JSON Schema:
Description: "answer to user query"
Update Flow State:
Updates the flow state with the generated JSON output.
Example:
Add an Iteration node and connect it to the output of the LLM node.
Purpose: Iterates over each item in the generated JSON array from LLM node.
Key Input Parameters:
Array Input:
The array to iterate over. Set to the answers from the saved state:
This means the node will loop through each event in the answers array.
Inside the loop, add a HTTP node.
Purpose: For each item in the array, sends an HTTP POST request to a specified endpoint (e.g., http://localhost:5566/events
).
Key Input Parameters:
Method:
POST
(default for this use case).
URL:
The endpoint to send data to.
Example:
Headers:
(Optional) Add any required HTTP headers (e.g., for authentication).
Query Params:
(Optional) Add any query parameters if needed.
Body Type:
json
(default): Sends the body as JSON.
Body:
The data to send in the request body.
Set to the current item in the loop:
Response Type:
json
(default): Expects a JSON response.
User Input:
Flow:
Start node receives the input.
LLM node generates a JSON array of events.
Loop node iterates through each event.
HTTP node create each event via the API.
Design Guidelines:
Clear Output Schema: Define the expected structure for the LLM output to ensure reliable downstream processing.
Common Use Cases:
Event Processing: Collect and send event data to a calendar or event management system.
Bulk Data Entry: Generate and submit multiple records to a database or API.
Automated Notifications: Send personalized messages or alerts for each item in a list.