Skip to content

Allow empty response #3105

@dconathan

Description

@dconathan

Initial Checks

Description

There are use cases where it is legitimate for the model to return an empty response. Currently this causes agents to crash in unintuitive ways (Pydantic AI retries the request and then the retry request fails with a Bad Request error). In prior versions this just raised a UnexpectedModelBehavior: Received empty model response

Relevant retry code is here:

if not self.model_response.parts:
# we got an empty response.
# this sometimes happens with anthropic (and perhaps other models)
# when the model has already returned text along side tool calls

Current behavior causes the request to retry which then for me results in a BadRequestError:

AzureException BadRequestError - Invalid value for 'content': expected a string, got null.

An example "legitimate" use case is if you have a "relevant text extractor" agent and it is instructed to only extract relevant verbatim text from the input and to return no text if there is no relevant text (see example).

Currently an easy workaround is to prompt the model to return the text "no relevant text" instead of an empty string - but this feels like a hacky workaround.

Is Pydantic AI intentionally making the opinionated choice that "there is no valid reason a model could return an empty response" ? What if I have fine-tuned a model to respond with empty strings in certain scenarios? Should we have an opt in like allow_empty_response=True in model or agent settings?

Example Code

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider


agent = Agent(
    model=OpenAIChatModel(
        "azure/gpt-5-nano",
        provider=OpenAIProvider(
            base_url="...",
        ),
    ),
    instructions="You are a text extractor agent. You always respond with verbatim text from the provided context that is relevant to George Washington. If there is no relevant text, respond with an empty string. Do not provide any additional commentary or information.",
)

text = """Taylor Alison Swift (born December 13, 1989) is an American singer-songwriter. Known for her autobiographical songwriting, artistic reinventions, and cultural impact, Swift is the highest-grossing live music artist, the wealthiest female musician, and one of the best-selling music artists of all time.

Swift signed with Big Machine Records in 2005 and debuted as a country singer with the albums Taylor Swift (2006) and Fearless (2008). The singles "Teardrops on My Guitar", "Love Story", and "You Belong with Me" found crossover success on country and pop radio formats. Speak Now (2010) expanded her country pop sound with rock influences, and Red (2012) featured a pop-friendly production. She recalibrated her artistic identity from country to pop with the synth-pop album 1989 (2014); ensuing media scrutiny inspired the hip-hop-imbued Reputation (2017). Through the 2010s, she accumulated the US Billboard Hot 100 number-one singles "We Are Never Ever Getting Back Together", "Shake It Off", "Blank Space", "Bad Blood", and "Look What You Made Me Do"."""


agent.run_sync(text)

Python, Pydantic AI & LLM client version

Python 3.13.7
Pydantic 2.11.10
Pydantic AI 1.0.15
OpenAI 2.2.0

Backend is Azure OpenAI endpoint through a LiteLLM proxy server.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions