Error: 'text content blocks must be non-empty' when using Claude Agent with Pydantic Response Model

Hello Agno community! I’ve run into an issue with content generation using an Agent with Claude and a Pydantic response model. Here’s what I’m working with:

from phi.agent import Agent
from phi.model.anthropic import Claude
from pydantic import BaseModel, Field
from typing import List, Optional, Literal

class Source(BaseModel):
    """Represents a content source"""
    url: str
    title: str
    content: str
    source_type: Literal["arxiv", "web", "wikipedia", "news"]
    relevance_score: float = Field(default=1.0)
    reasoning: str

class Section(BaseModel):
    """Represents an article section"""
    title: str
    description: str
    content: Optional[str] = None
    sources: List[Source] = Field(default_factory=list)

class Article(BaseModel):
    """Represents a complete article"""
    title: str
    sections: List[Section]
    main_sources: List[Source] = Field(default_factory=list)
    internal_links: List[str] = Field(default_factory=list)

writer_agent = Agent(
    name="Writer Agent",
    model=Claude(id=CLAUDE_MODEL_ID, api_key=ANTHROPIC_API_KEY),
    team=[search_strategist, researcher_agent, content_planner, content_writer],
    response_model=Article
)

When trying to generate content, I’m getting the following error:

Error during content generation: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}

What makes this particularly interesting is that this error occurs intermittently - most of the time the agent works perfectly fine. Has anyone else encountered this intermittent issue?

Note: I haven’t upgrade to Agno yet.

Hi @Southern_Push2935
Thanks for reaching out and for using Agno! I’ve looped in the right engineers to help with your question. We usually respond within 24 hours, but if this is urgent, just let us know, and we’ll do our best to prioritize it.
Appreciate your patience—we’ll get back to you soon! :smile:

Hi @Southern_Push2935
Which version of phidata are you one? I know we fixed a lot of issues around Claude in our migration to agno, so you might be better of migrating? Here are our docs for the migration.

This particular error occurs when an empty message is sent to claude. I tested this is my own example (your example wasn’t the full code I assume), and it worked for me in agno.

Thank you @Dirk Yes, I’m working on the migration. However, I wanted to bring this up because I’ve seen the issue reported in other forums regarding Claude and empty message errors, but without a definitive explanation for why it occurs intermittently - the messages are definitely not empty when I debug the program.

Again thanks for raising. I hope it is resolved going forward.