Skip to content

Bug: run_ag_ui() doesn't use agent's system promptsΒ #3168

@anishathalye

Description

@anishathalye

Initial Checks

Description

When using Agent.to_ag_ui(), the agent's system prompts (defined via @agent.system_prompt or passed to the Agent() constructor) are not applied to the conversation. This causes the agent to behave differently than when using agent.run() directly.

Briefly, the root cause is:

  1. run_ag_ui() handles an AG-UI request by converting the AG-UI history into a Pydantic AI one (via _messages_from_ag_ui), so this produces a list[ModelMessage] that is missing the Agent's system prompts.
  2. UserPromptNode.run() doesn't prepend the system prompt if there is already a message history; it only prepends the system prompt if there is no message history (and the user_prompt argument is specified; but run_ag_ui() specifies user_prompt=None).

I am not sure what is the cleanest way to fix this, so I'm opening an issue rather than submitting a PR.

It's possible that Agent.to_a2a() has a similar issue; I haven't read through that code very closely, but it looks like it might be structured in a similar way, where it converts an A2A history into a Pydantic AI one.

Example Code

import asyncio

from ag_ui.core import RunAgentInput, UserMessage
from pydantic_ai import Agent
from pydantic_ai.ag_ui import run_ag_ui

agent = Agent("openai:gpt-4o", system_prompt="You are a helpful pirate assistant. Always respond like a pirate.")

# This works - system prompt is applied
result = agent.run_sync("Hello!")
print(result.output)  # Response in pirate style


# When accessing via AG-UI protocol, responses are NOT in pirate style
async def demo_ag_ui():
    run_input = RunAgentInput(
        thread_id="1",
        run_id="2",
        state=None,
        messages=[UserMessage(id="3", content="Hello!")],
        tools=[],
        context=[],
        forwarded_props=None,
    )
    response_iter = run_ag_ui(agent, run_input)
    async for response in response_iter:
        print(response)  # Response NOT in pirate style


asyncio.run(demo_ag_ui())

Python, Pydantic AI & LLM client version

$ python --version
Python 3.13.7
$ python -c 'import pydantic; print(pydantic.__version__)'
2.12.2
$ python -c 'import pydantic_ai; print(pydantic_ai.__version__)'
1.0.19.dev1+2631d9b8

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