Linkup’s structured output feature allows you to receive responses in a custom format that you define. This is particularly useful when you need to integrate Linkup’s responses directly into your application’s data structure or when you want to ensure consistency in the response format.
Provide a JSON schema string in the structuredOutputSchema parameter
The API will return a response that strictly follows your schema
Write your query so that its answer contains the information requested in the
structuredOutputSchema- the system will use the query response to fill the
output
Let’s look at some simple examples that demonstrate how to use structured outputs for different use cases:
Company Revenue Example
This example extracts company classification information:
Copy
Ask AI
from linkup import LinkupClientclient = LinkupClient(api_key="YOUR_API_KEY")schema = """{ "type": "object", "properties": { "companyName": { "type": "string", "description": "The name of the company" }, "revenueAmount": { "type": "number", "description": "The revenue amount" }, "fiscalYear": { "type": "string", "description": "The fiscal year for this revenue" } }}"""response = client.search( query="What is Microsoft's 2024 revenue?", depth="deep", output_type="structured", structured_output_schema=schema)print(response)
This example shows how to retrieve basic information about a movie:
Copy
Ask AI
from linkup import LinkupClientclient = LinkupClient(api_key="YOUR_API_KEY")schema = """{ "type": "object", "properties": { "movieTitle": { "type": "string", "description": "The title of the movie" }, "director": { "type": "string", "description": "The director of the movie" }, "releaseYear": { "type": "integer", "description": "The year the movie was released" }, "boxOfficeRevenue": { "type": "number", "description": "The worldwide box office revenue in USD" } }, "required": ["movieTitle", "director", "releaseYear", "boxOfficeRevenue"]}"""response = client.search( query="What was the director, release year, and box office revenue for The Matrix?", depth="deep", output_type="structured", structured_output_schema=schema)print(response)
Example response:
Copy
Ask AI
{ "movieTitle": "The Matrix", "director": "Lana and Lilly Wachowski", "releaseYear": 1999, "boxOfficeRevenue": 465300000}
Weather Information Example
This example retrieves weather information for travel planning:
Copy
Ask AI
from linkup import LinkupClientclient = LinkupClient(api_key="YOUR_API_KEY")schema = """{ "type": "object", "properties": { "city": { "type": "string", "description": "The name of the city" }, "averageTemperature": { "type": "object", "properties": { "summer": { "type": "number", "description": "Average temperature in summer (°C)" }, "winter": { "type": "number", "description": "Average temperature in winter (°C)" } } }, "annualRainfall": { "type": "number", "description": "Annual rainfall in millimeters" }, "bestTimeToVisit": { "type": "array", "items": { "type": "string" }, "description": "The recommended months to visit" } }}"""response = client.search( query="What is the average temperature, rainfall, and best time to visit Tokyo?", depth="deep", output_type="structured", structured_output_schema=schema)print(response)
To make sure that the key fields you defined are filled, you can mark them as required in your json schema.
Required Fields Highlight
Copy
Ask AI
{ "type": "object", "properties": { "movieTitle": { "type": "string", "description": "The title of the movie" }, "director": { "type": "string", "description": "The director of the movie" }, "releaseYear": { "type": "integer", "description": "The year the movie was released" } }, "required": ["movieTitle", "director"]}
Write your query so that its answer contains the information requested in the structuredOutputSchema- the system will use the query response to fill the output
Provide clear context in your query and use explicit instructions