How do I convert OpenAI-style an array of dicts (e.g. {'role': 'user', 'content':…
- User
How do I convert OpenAI-style an array of dicts (e.g.
{'role': 'user', 'content': 'Hi.'}) into PydanticAI objcets that can be passed to Agent.run as message_history? Assistant
To convert an array of dictionaries into Pydantic objects that can be passed to
Agent.runasmessage_history, you will first need to define a Pydantic model that matches the structure of your dictionaries. Then, you can use this model to parse your array of dictionaries into a list of Pydantic objects.Here's a step-by-step guide on how to achieve this:
Step 1: Install Required Libraries
Ensure you have Pydantic installed. If not, you can install it using pip:
pip install pydanticStep 2: Define the Pydantic Model
Define a Pydantic model that represents the structure of your dictionaries. For the example you provided (
{'role': 'user', 'content': 'Hi.'}), your model could look like this:from pydantic import BaseModel class Message(BaseModel): role: str content: strStep 3: Convert Dictionaries to Pydantic Objects
Now, you can convert your array of dictionaries into a list of Pydantic
Messageobjects. Here's how you can do it:# Example array of dictionaries messages_dict = [ {'role': 'user', 'content': 'Hi.'}, {'role': 'assistant', 'content': 'Hello! How can I assist you?'}, # Add more messages as needed ] # Convert dictionaries to Pydantic Message objects messages = [Message(**msg) for msg in messages_dict]Step 4: Pass to
Agent.runAfter converting your array of dictionaries into Pydantic objects, you can pass this list to
Agent.runasmessage_history. The exact implementation might vary depending on howAgent.runis defined in your context, but it should look something like this:from your_module import Agent # Import Agent from wherever it's defined # Assuming Agent.run is a method that accepts message_history agent = Agent() # Initialize your agent response = agent.run(message_history=messages)Full Example
Here's a full example that includes defining a simple
Agentclass for demonstration purposes:from pydantic import BaseModel class Message(BaseModel): role: str content: str class Agent: def run(self, message_history): # Example method that prints out the message history for msg in message_history: print(f"Role: {msg.role}, Content: {msg.content}") return "Messages processed." # Example usage if __name__ == "__main__": messages_dict = [ {'role': 'user', 'content': 'Hi.'}, {'role': 'assistant', 'content': 'Hello! How can I assist you?'}, ] messages = [Message(**msg) for msg in messages_dict] agent = Agent() response = agent.run(message_history=messages) print(response)This example demonstrates how to convert an array of dictionaries into Pydantic objects and pass them to a method (
Agent.run) asmessage_history. Adjust theAgentclass and itsrunmethod according to your actual implementation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.