phidatahq / phidata

Build AI Agents with memory, knowledge, tools and reasoning. Chat with them using a beautiful Agent UI.
https://docs.phidata.com
Mozilla Public License 2.0
15.58k stars 2.14k forks source link

Error when using Structured Output #1433

Open skelleex opened 1 week ago

skelleex commented 1 week ago

I am trying to do a structured output but I am getting an error. TypeError: You tried to pass a BaseModel class to chat.completions.create(); You must use beta.chat.completions.parse() instead. Maybe there is a need for an update for phidata. Note I am using the model model=OpenAIChat(id="gpt-4o-2024-08-06"), with structure outputs being True.

ysolanky commented 1 week ago

Hey @skelleex ! Can you please try updating your phidata version and running the structured_output_agent in this file. Phidata supports the use of beta.chat.completions.parse() for structured output.

skelleex commented 1 week ago

Hey @ysolanky I have the most recent versions of phidata and the file works. However I am still getting the error. I am assuming now that its a pydantic schema issue

ysolanky commented 1 week ago

@skelleex can you please share your Agent config and the Pydantic model. Also confirming if the cookbook example I shared is working for you

skelleex commented 1 week ago

@ysolanky cookbook example works

here is the agent config

`from pydantic import BaseModel, Field from typing import List, Optional from datetime import datetime from phi.agent import Agent, RunResponse from phi.model.anthropic import Claude from phi.model.openai import OpenAIChat from rich.pretty import pprint

class RFQDocument(BaseModel): rfq_number: str = Field(..., description="Unique identifier for the RFQ, following format iGOV/RFQ/XXX/YY")

issue_date: datetime = Field(..., description="Official date when the RFQ is issued")

submission_deadline: datetime = Field(..., description="Final date and time for submission of proposals")

contact_details: dict = Field(..., description="Primary contact information for RFQ inquiries including email and title")

submission_portal: str = Field(..., description="URL where quotations should be submitted")

background: str = Field(..., description="Detailed company background including history, mandate, and current operations")

managed_systems: List[dict] = Field(
    ..., 
    description="List of ICT systems, platforms and data currently managed by the company"
)

service_requirements: List[str] = Field(
    ...,
    description="Specific membership and subscription service requirements including online portal access, training, and support services"
)

service_objectives: List[str] = Field(
    ...,
    description="Key objectives that the company expects to achieve through the subscription service"
)

price_requirements: dict = Field(
    ...,
    description="Price constraints and requirements including maximum budget of USD 70,000 and fixed price requirement"
)

submission_requirements: List[str] = Field(
    ...,
    description="Required documents and presentation requirements for quotation submission"
)

authorization_requirements: List[str] = Field(
    ...,
    description="Requirements for authorization of quotations including signature and company details"
)

pricing_rules: List[str] = Field(
    ...,
    description="Rules for quoted prices including currency requirements and price validity"
)

payment_terms: Optional[str] = Field(
    None,
    description="Terms of payment to be agreed upon with the vendor"
)

validity_period: dict = Field(
    ...,
    description="Quotation validity period of 90 days and rules for extension"
)

evaluation_criteria: dict = Field(
    ...,
    description="Technical and financial criteria for bid evaluation including scoring breakdown"
)

security_requirements: Optional[List[str]] = Field(
    ...,
    description="Data security and confidentiality requirements for handling company information"
)

structured_output_agent = Agent( model=OpenAIChat(id="gpt-4o-2024-08-06"), description="You write RFQ procurement documents.", response_model=RFQDocument, structured_outputs=True, )

structured_output_agent_response: RunResponse = structured_output_agent.run(prompt) pprint(structured_output_agent.content)`