Skip to content

Conversation

DouweM
Copy link
Collaborator

@DouweM DouweM commented Oct 8, 2025

Example based on https://ai.pydantic.dev/ag-ui/#events, using ag_ui.core.CustomEvent and ToolReturn:

@agent.tool_plain
async def yield_custom() -> AsyncIterator[CustomEvent | ToolReturn]:
    yield CustomEvent(
        type=EventType.CUSTOM,
        name='custom_event1',
        value={'key1': 'value1'},
    )
    yield CustomEvent(
        type=EventType.CUSTOM,
        name='custom_event2',
        value={'key2': 'value2'},
    )
    yield ToolReturn('Done')

If the yielded item is a pydantic_ai.messages.CustomEvent, it'll show up in the event streams as-is. If it's ToolReturn, it's the tool return value. If it's Anything else, it'll be wrapped in a CustomEvent.

AG-UI integration automatically forwards ag_ui.core.CustomEvent payloads to the frontend.


To do:

@DouweM DouweM self-assigned this Oct 8, 2025
@github-actions
Copy link

github-actions bot commented Oct 8, 2025

Docs Preview

commit: d349a7a
Preview URL: https://bd566ace-pydantic-ai-previews.pydantic.workers.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send custom events from tool function, for AG-UI and event_stream_handler

1 participant