Skip to content

Allow running agent with ModelResponse as last message #3184

@fortzi

Description

@fortzi

Description

Sometimes it's useful to have an assistant message be the last message in the message history.
For example, you have a chat bot that sends another message after some time that the user doesn't respond:

User: Hi can I get help with X?
Agent: Sure, can you provide detail Y?
<some time passes>
Agent: If you don't have this detail I can do Z for you

Since Agent.run() (and Agent.iter()) only get a user prompt as an input, this is impossible.
Previously, it was possible to put some dummy prompt in there, and remove it in message history processing. Since v1.0.8 this is no longer possible due to a new validation in pydantic_ai._agent_graph._process_message_history that raises exceptions.UserError('Processed history must end with a ModelRequest.')

Is this validation mission critical, or is mostly for detecting probably-wrong history processing?

Maybe this validation can be opted out of? WDYT?

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions