Skip to content

Forbid first ModelResponse message #2992

@mshavliuk

Description

@mshavliuk

Initial Checks

Description

An invalid history of messages starting with ModelResponse passes validation, but causes API errors down the line (see below)

Example Code

The following code does not raise any validation errors and instead fails after receiving error code from Bedrock API:

import boto3
from pydantic_ai import Agent
from pydantic_ai.messages import TextPart, ModelResponse
from pydantic_ai.models.bedrock import BedrockConverseModel
from pydantic_ai.providers.bedrock import BedrockProvider

model = BedrockConverseModel(
    "eu.amazon.nova-lite-v1:0",
    provider=BedrockProvider(bedrock_client=boto3.client("bedrock-runtime")),
)
agent = Agent(model, output_type=str)

truncated_history = [ModelResponse(parts=[TextPart(content="ai response")])]

response = agent.run_sync("hello", message_history=truncated_history)
print(response.output)

And fails with exception

botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: A conversation must start with a user message. Try again with a conversation that starts with a user message.

Python, Pydantic AI & LLM client version

pydantic-ai: 1.0.10
model client: BedrockConverseModel

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