Skip to content

ModelResponse().cost() is broken for pydantic-ai>=1.0.3, because model_name is empty when using an event_handler #2943

@marhoy

Description

@marhoy

Initial Checks

Description

When using an OpenAIChatModel and an event_stream_handler, the model_name field of the ModelResponse objects are empty for 1.0.3 <= pydantic_ai <= 1.0.8. It works as expected for version 1.0.2. It also works for newer versions if I'm not using an event handler.

The example code below illustrates the problem, (sorry for not revealing my AsyncAzureOpenAI client 😉).

Example Code

from collections.abc import AsyncIterable

from pydantic_ai import Agent, RunContext
from pydantic_ai.messages import AgentStreamEvent, ModelResponse
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.azure import AzureProvider

from my_own_app.llm_models import client

model = OpenAIChatModel(
    model_name="gpt-4o-2024-05-13",
    provider=AzureProvider(openai_client=client),
)
agent = Agent(model)


async def event_stream_handler(
    _ctx: RunContext,
    event_stream: AsyncIterable[AgentStreamEvent],
) -> None:
    pass


response_with_handler = await agent.run(
    "Who was Albert Einstein?",
    event_stream_handler=event_stream_handler,
)

[
    msg.cost()
    for msg in response_with_handler.all_messages()
    if isinstance(msg, ModelResponse)
]

Python, Pydantic AI & LLM client version

python=3.13
pydantic_ai=1.0.8

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions