Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationError in structured_llm.invoke when generating analysts: Missing required fields (affiliation and description) #690

Open
Mu7annad0 opened this issue Jan 12, 2025 · 0 comments

Comments

@Mu7annad0
Copy link

I am encountering a ValidationError when using the structured_llm.invoke method to generate a list of analysts. The error indicates that the required fields affiliation and description are missing for each analyst in the Perspective model. Here's the relevant code snippet:

analysts_response = structured_llm.invoke(
    [SystemMessage(content=system_message)] + [HumanMessage(content=human_msg)])

Error Message:

ValidationError: 6 validation errors for Perspective
analysts.0.affiliation
Field required [type=missing, input_value={'name': 'Dr. Anya Sharma...e': 'Lead AI Scientist'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
analysts.0.description
Field required [type=missing, input_value={'name': 'Dr. Anya Sharma...e': 'Lead AI Scientist'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
analysts.1.affiliation
Field required [type=missing, input_value={'role': 'Chief Software ...name': 'Mr. Ben Carter'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
analysts.1.description
Field required [type=missing, input_value={'role': 'Chief Software ...name': 'Mr. Ben Carter'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
analysts.2.affiliation
Field required [type=missing, input_value={'role': 'Senior Tech Pol...ame': 'Ms. Chloe Davis'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
analysts.2.description
Field required [type=missing, input_value={'role': 'Senior Tech Pol...ame': 'Ms. Chloe Davis'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing

Expected Behavior:
The structured_llm.invoke method should return a valid Perspective object with all required fields (affiliation, name, role, and description) populated for each analyst.

Actual Behavior:
The method fails with a ValidationError because the affiliation and description fields are missing in the generated output.

Code Snippet:

class Analyst(BaseModel):
    affiliation: str = Field(
        description="Primary affiliation of the analyst.",
    )
    name: str = Field(
        description="Name of the analyst."
    )
    role: str = Field(
        description="Role of the analyst in the context of the topic.",
    )
    description: str = Field(
        description="Description of the analyst focus, concerns, and motives.",
    )
    @property
    def persona(self) -> str:
        return f"Name: {self.name}\nRole: {self.role}\nAffiliation: {self.affiliation}\nDescription: {self.description}\n"

class Perspective(BaseModel):
    analysts: List[Analyst] = Field(
        description="Comprehensive list of analysts with their roles and affiliations.",
    )

def create_analysts(state: GenerateAnalystsState):
    """Create analysts for a given topic"""

    topic = state["topic"]
    max_analysts = state["max_analysts"]
    human_analyst_feedback = state.get("human_analyst_feedback", '')

    structured_llm = llm.with_structured_output(Perspective)

    system_message = analyst_instructions.format(
        topic=topic,
        human_analyst_feedback=human_analyst_feedback,
        max_analysts=max_analysts,
    )

    human_msg = """Generate the set of analysts."""

    analysts_response = structured_llm.invoke(
        [SystemMessage(content=system_message)] + [HumanMessage(content=human_msg)]
    )

    return {
        "analysts": analysts_response.analysts
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant