Agent with Object Response

Get Structured and Programmable Results from Your Agent

It is crucial for an agent’s outputs to be usable. A generated text may not always be enough. Sometimes, we may need to perform analysis and extract specific data points.

In such cases, using ObjectResponse simplifies your work by returning structured objects instead of unstructured text.


Agent with Object Response

This agent will fetch OpenAI developments like the previous example, but instead of returning plain text, it will return structured data that can be easily processed.

agent_with_object_response.py

pythonCopyEditfrom beaconlabs import Agent, Task
from beaconlabs.client.tools import Search

from beaconlabs import ObjectResponse  # Importing Object Response

class ANew(ObjectResponse):
  title: str
  content: str

class News(ObjectResponse):
  news: list[ANew]  # We want to get a list of ANew objects

task = Task(
  "Find the latest OpenAI developments on the internet.", 
  tools=[Search],
  response_format=News  # Specifying the Response format we want
)

agent = Agent("Reporter")

agent.print_do(task)

Processing the Results

When you want to use the news results:

agent_with_object_response.py


Running the Agent

To run the agent, install dependencies and export your OPENAI_API_KEY.

1. Set Up Your Virtual Environment

Mac / Windows


2. Install Dependencies

Mac / Windows


3. Set Up Your OpenAI API Key

.env File


4. Run the Agent to Execute the Task


✅ You’ve successfully created an agent that returns structured data using Beacon Labs! 🚀

Last updated