Skip to content

Not parsing/producing a structured output. Using LiteLLM. #2088

@aiorga-sherpas

Description

@aiorga-sherpas

Describe the bug
The agent produces a Pydantic validation error when trying to get structured output. It may be a postprocessing error because its a valid answer but contained between ```json ```.

To Reproduce

class Album(BaseModel):
    """Structured data representing an album."""

    name: str = Field(description="The name of the album.")
    artist: str = Field(description="The artist of the album.")
    year: int = Field(description="The year of the album.")
    genre: str = Field(description="The genre of the album.")


class Albums(BaseModel):
    """Structured data representing a list of albums."""

    albums: List[Album] = Field(description="A list of albums.")


async def create_album_agent() -> LlmAgent:
    """
    Creates an album recommendation agent that returns structured album data.

    Returns:
        LlmAgent: Configured agent for album recommendations
    """
    return LlmAgent(
        name="AlbumAgent",
        model=LiteLlm(model="openrouter/anthropic/claude-sonnet-4"),
        description="Agent that provides album recommendations based on user preferences",
        instruction="""You are an Album Recommendation Agent. Given a music preference, genre, or artist, provide a curated list of 5-8 relevant albums.

Guidelines:
1. Include a diverse mix of classic and contemporary albums
2. Provide accurate album names, artists, years, and genres
3. Consider the user's specific request (genre, mood, artist similarity, etc.)
4. Include both popular and lesser-known gems
5. Ensure accurate information - double-check album names and release years

Respond ONLY with a JSON object that matches the Albums schema format:
{
  "albums": [
    {
      "name": "Album Name",
      "artist": "Artist Name", 
      "year": 2023,
      "genre": "Genre Name"
    }
  ]
}

Do not include any additional text or explanation.""",
        output_schema=Albums,
        output_key="recommended_albums",
        include_contents="none",
        disallow_transfer_to_parent=True,
        disallow_transfer_to_peers=True,
    )

The error.

 Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='```json\n{\n  "albums": ...on"\n    }\n  ]\n}\n```', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/json_invalid

Metadata

Metadata

Labels

answered[Status] This issue has been answered by the maintainermodels[Component] Issues related to model supportrequest clarification[Status] The maintainer need clarification or more information from the author

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions