Skip to content

Streaming with tools using VercelAIEventStream with Anthropic extended thinking models raises error #3748

@Light2Dark

Description

@Light2Dark

Initial Checks

Description

Sorry that this might be hard to reproduce. I've tried a few ways but putting this up first in case anyone runs into a similar error. It seems like there is a missing signature which causes the following error

Error

    status_code: 400
    body: {
        'type': 'error',
        'error': {
            'type': 'invalid_request_error',
            'message': "messages.1.content.0.type: Expected `thinking` or `redacted_thinking`,
                       but found `text`. When `thinking` is enabled, a final `assistant` message
                       must start with a thinking block..."
        }
    }

Workaround

To fix this, I modified pydantic_ai/ui/vercel_ai/_event_stream.py to pass the signature in provider_metadata

    async def handle_thinking_end(
        self, part: ThinkingPart, followed_by_thinking: bool = False
    ) -> AsyncIterator[BaseChunk]:
        yield ReasoningEndChunk(id=self.message_id)  # <-- part.signature not passed
    async def handle_thinking_end(
        self, part: ThinkingPart, followed_by_thinking: bool = False
    ) -> AsyncIterator[BaseChunk]:
        provider_metadata = None
        if part.signature:
            pydantic_ai_meta = {"signature": part.signature}
            if part.provider_name:
                pydantic_ai_meta["provider_name"] = part.provider_name
            if part.id:
                pydantic_ai_meta["id"] = part.id
            provider_metadata = {"pydantic_ai": pydantic_ai_meta}

        yield ReasoningEndChunk(
            id=self.message_id, provider_metadata=provider_metadata
        )

Example Code

Python, Pydantic AI & LLM client version

python 3.13
pydantic 1.34.0

Metadata

Metadata

Assignees

No one assigned

    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