Skip to content

The Agent.system_prompt is discarded when message_history contain ModelResponse #1032

Open
@oscar-broman

Description

@oscar-broman

Initial Checks

  • I confirm that I'm using the latest version of Pydantic AI

Description

The system prompt is seemingly ignored when commenting out the line mentioned in the MRE below.

Example Code

from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.messages import TextPart, ModelResponse
from pydantic_ai.models.openai import OpenAIModel

async def test_system_prompt_issue() -> None:
    openai_key = ""

    model = OpenAIModel(model_name="gpt-4o-mini", openai_client=AsyncOpenAI(api_key=openai_key))

    agent = Agent(
        model=model,
        system_prompt="Respond with the user's last message in upper-case",
    )

    messages = [
        # If this is commented out, the system prompt will not be overridden and the test will pass
        ModelResponse(parts=[TextPart(content="Hi, write something and I'll repeat it")]),
    ]

    result = await agent.run(
        user_prompt="Test",
        message_history=messages,
        result_type=str,
    )

    assert result.all_messages()[-1].parts[0].content == "TEST"

Python, Pydantic AI & LLM client version

Python 3.12.9
pydantic-ai 0.0.30
openai 1.65.2

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