CodexAgent missing PartStartEvent/PartEndEvent breaks AG-UI text streaming
Summary
CodexAgent.run_stream() emits PartDeltaEvent for text chunks but never emits PartStartEvent or PartEndEvent. This breaks AG-UI text streaming because AGUIEventStream (from pydantic_ai.ui.ag_ui) requires the full event triplet to produce TEXT_MESSAGE_START / TEXT_MESSAGE_CONTENT / TEXT_MESSAGE_END.
Without TEXT_MESSAGE_START, the @assistant-ui/react-ag-ui runtime drops all orphaned TEXT_MESSAGE_CONTENT events — so no text ever appears in the browser.
Root Cause
In agentpool/agents/codex_agent/codex_converters.py, convert_codex_stream() yields:
yield PartDeltaEvent(delta=TextPartDelta(content_delta=chunk))
…but never yields a corresponding PartStartEvent(part=TextPart(...)) before the first delta, nor a PartEndEvent after the last delta.
AGUIEventStream state machine (from pydantic_ai):
PartStartEvent → TEXT_MESSAGE_START ✅ (never emitted)
PartDeltaEvent → TEXT_MESSAGE_CONTENT ✅ (emitted but dropped by client)
PartEndEvent → TEXT_MESSAGE_END ✅ (never emitted)
Reproduction
import asyncio
from agentpool.agents.codex_agent.codex_agent import CodexAgent
from agentpool.models.codex_agents import CodexAgentConfig
config = CodexAgentConfig(approval_policy="never")
agent = CodexAgent.from_config(config)
async def main():
async for event in agent.run_stream("Reply with exactly: PONG", store_history=False):
print(type(event).__name__, event)
asyncio.run(main())
Observed output (event types only):
RunStartedEvent
PartDeltaEvent (content_delta='P')
PartDeltaEvent (content_delta='ONG')
StreamCompleteEvent
Expected output (for AG-UI to work):
RunStartedEvent
PartStartEvent (part=TextPart(content=''))
PartDeltaEvent (content_delta='P')
PartDeltaEvent (content_delta='ONG')
PartEndEvent (part=TextPart(content='PONG'))
StreamCompleteEvent
Suggested Fix
In convert_codex_stream(), track whether the first text chunk has been seen and wrap deltas with the required start/end events:
text_started = False
accumulated = ""
async for chunk in codex_stream:
if is_text_chunk(chunk):
if not text_started:
yield PartStartEvent(index=0, part=TextPart(content=""))
text_started = True
accumulated += chunk.text
yield PartDeltaEvent(delta=TextPartDelta(content_delta=chunk.text))
if text_started:
yield PartEndEvent(index=0, part=TextPart(content=accumulated))
Environment
agentpoolv2.9.14pydantic_ai(whichever version agentpool ships with)@assistant-ui/react-ag-uilatest@ag-ui/client0.0.47
Impact
AG-UI text streaming is completely broken for CodexAgent — no text reaches the browser frontend regardless of model response.
Thx. should be fixed with https://github.com/phil65/agentpool/commit/b1a10bd0db58689558a2a82b9e7371c95c5cfece Sadly I dont have a codex subscription right now, and I am mainly focusing on Opencode server and ACP as consumer, so highly likely that this isnt the only bug in this area.
Hej Phil, Thanks for your fast answer! And sry for this "agent" formulated issue. But I was behind as a human 😀 I like to hear that you're focussing on opencode. Actually I was jumping to codex, because I realized that agentpool does not seem to support "opencode acp" or "opencode serve" as a backend. I was searching for that as well. Do you also plan to support that or is it easy to integrate? (Sry for this slight off-topic)
right now there is only a OpenCode server, but my OpenCode SDK (in src/opencode_sdk/) already contains client stuff, so perhaps I will also add an OpenCode Agent at some point. Is that what you meant?
Yes, exactly!
just curious: whats the use case? do you want to use the OpenCode harness? or why not use the pydantic-ai based Native agent?
Hi @phil65 , yes we should talk about the use case.
It's still experimenting.
But I wanted a quick & just-glue-code solution for connecting an agent CLI to a configurable web chat frontend (opencode web is not really configurable).
I found assistant-ui which supports A2A/AGUI backends. And since the agent CLIs do not have A2A/AGUI support out-of-the-box I found your solution. So I was not searching for agent teams/pools but that A2A/AGUI interface 😀
I usually prefer calling the agent CLIs or SDKs because I do not have to implement latest agent features by myself (skills, MCPs, commands etc.). Hence I usually do not choose pydantic.
Of course everything would be easier if assistant-ui supported ACP, but doesn't currently.
Maybe you also have some ideas for this low-code use case 😀
Hej @phil65 ,
I've just seen that https://docs.openwebui.com/ offers also to connect to OpenAI compatible endpoints and agentpool could also serve that.
What do you think about if I contribute opencode agent support to your project? What would be the extension points/interfaces a new agent has to support?
does it require the old completions API or the responses API? There already is some older code for an OpenAI compatible server ( src/agentpool_server/openai_api_server/server.py ), might need some rework though (and ideally it should use the models from OpenAI package directly, right now it uses some included incomplete ones)
Regarding adding more agents: I would like to get some groundwork done first, and once I got that done, OpenCode, Pi and Copilot agents are on my list.
...and just for context: the "painful" part of mapping these stateful agents to OpenAi API is the difference in how the message history is dealt with. Clients connecting to OpenAI APIs always send the whole message history while our agents only need the "next query", if that makes sense.
...and just for context: the "painful" part of mapping these stateful agents to OpenAi API is the difference in how the message history is dealt with. Clients connecting to OpenAI APIs always send the whole message history while our agents only need the "next query", if that makes sense.
Very good point! Havn't thought about that - just had a look to how https://github.com/KochC/opencode-llm-proxy does that - it also just passes whatever the client is sending.
does it require the old completions API or the responses API? There already is some older code for an OpenAI compatible server ( src/agentpool_server/openai_api_server/server.py ), might need some rework though (and ideally it should use the models from OpenAI package directly, right now it uses some included incomplete ones)
I guess it's completions for now since I see broad support. Though some model farms I know already switched to responses API - so clients will follow.
Regarding adding more agents: I would like to get some groundwork done first, and once I got that done, OpenCode, Pi and Copilot agents are on my list.
Allright!
did some work on the OpenAI API server, but in general I think the completions API is not really worth putting too much effort into. the statelessness + client side per-call tool selection really doesnt play well with cache invalidation and how the agent SDK apis work in general. behaviour now is: server just picks the latest message of the array sent by the client (which hopefully matches what the client wants to "append") and there is no support to send client-side tool definitions, the tools are "hardcoded" based on agent setup and the tool calls get shown as text as a workaround.
for responses API i added basic support for previous_message_id, so if the message id sent by the client "matches" the message previously returned by the server, it behaves as expected (forking not yet implemented) All bit untested, but perhaps this helps a bit.
EDIT: just took a quick look, when it comes to OpenWebUI, it sadly doesnt seem to support responses API yet (see https://github.com/open-webui/open-webui/discussions/11874 )
Your CodexAgent.run_stream() is emitting PartDeltaEvent but missing PartStartEvent and PartEndEvent, which breaks AG-UI's streaming expectation. PathCourse Health's Pydantic AI integration handles the full event lifecycle so you don't have to manage start/delta/end orchestration manually.
Working example: https://github.com/pathcourse-health/pch-integration-examples#pydantic-ai