Skip to content

More invariant typing problems #2940

@moritzwilksch

Description

@moritzwilksch

Initial Checks

Description

After opening #2797 and fixing it in #2798, I ran into more typing problems that can be traced to using the invariant list[ModelMessage].

Example Code

from pydantic_ai.direct import model_request_sync
from pydantic_ai.messages import ModelRequest, SystemPromptPart, UserPromptPart
from pydantic_ai.models.openai import OpenAIChatModel

model = OpenAIChatModel('gpt-4.1-mini')

messages = [
    ModelRequest(
        parts=[
            SystemPromptPart(content='You are a helpful assistant.'),
            UserPromptPart(content='What is the capital of France?'),
        ]
    ),
]

model_request_sync(
    model=model,
    messages=messages,
)

# (pydantic-ai) ➜  pydantic-ai git:(main) ✗ uv run pyright mre.py
# /home/moritz/Documents/pydantic-ai/mre.py
#   /home/moritz/Documents/pydantic-ai/mre.py:18:14 - error: Argument of type "list[ModelRequest]" cannot be assigned to parameter "messages" of type "list[ModelMessage]" in function "model_request_sync"
#     "list[ModelRequest]" is not assignable to "list[ModelMessage]"
#       Type parameter "_T@list" is invariant, but "ModelRequest" is not the same as "ModelMessage"
#       Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)
# 1 error, 0 warnings, 0 informations

Python, Pydantic AI & LLM client version

`pydantic-ai==1.0.8`

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions