Skip to content

Using Function Model with streaming responses #2505

@tenzo

Description

@tenzo

Question

Hello, could anyone provide an example of using the FunctionModel for testing tools being called? I'm having various issues as I call it like:

with my_agent.override(
        model=FunctionModel(stream_function=call_my_tool)
):

And the call_my_tool should be an async iterator yielding one of 3 types:

StreamFunctionDef: TypeAlias = Callable[
    [list[ModelMessage], AgentInfo], AsyncIterator[Union[str, DeltaToolCalls, DeltaThinkingCalls]]
]

so I assume I should yield DeltaToolCalls (=dict[int, DeltaToolCall] but if my function returns something like:

        yield {
            1: DeltaToolCall(
                name='my_tool',
            )
        }

I'm having errors with iterator being exhausted:

ValueError: Stream function must return at least one item

Can anyone provide a working example with FunctionModel and iter in a test?
AFAIK there's no such example available.

Additional Context

PydanticAI 0.6.2
Python 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions