Skip to content

Python: [Bug]: Workflow fails with Anthropic models — assistant message prefill not supported #5008

@sphenry

Description

@sphenry

Description

When two agents backed by Anthropic AnthropicClient are connected via WorkflowBuilder.add_edge(), the first agent's output (assistant-role messages) is passed directly as context to the second agent. Anthropic's API rejects this because it requires the conversation to end with a user message.

This means any multi-agent workflow using Anthropic models fails on the second agent invocation. The issue is in AgentExecutor with the default context_mode="full" — it forwards the full conversation history including assistant messages without re-roling them for providers that don't support assistant prefill.

Expected Behavior:

The workflow should handle message role conversion automatically when passing context between agents, regardless of the downstream provider. Either:

AgentExecutor should detect that the target agent's provider doesn't support assistant prefill and re-role assistant messages as user messages, or
The WorkflowBuilder should insert a role-conversion step when connecting agents backed by Anthropic.

Code Sample

#!/usr/bin/env python3
"""
Minimal repro: Agent Framework workflow fails with Anthropic models.

When two agents are connected via WorkflowBuilder, the first agent's output
(assistant-role messages) is passed directly to the second agent. Anthropic's
API rejects this because the conversation must end with a user message.

Error:
    anthropic.BadRequestError: 400 - This model does not support assistant
    message prefill. The conversation must end with a user message.

Expected: The workflow should automatically handle message role conversion
when the downstream agent uses Anthropic.
"""

from __future__ import annotations

import asyncio

from agent_framework import AgentResponseUpdate, WorkflowBuilder
from agent_framework_anthropic import AnthropicClient
from dotenv import load_dotenv

load_dotenv()


async def main() -> None:
    client = AnthropicClient(model_id="claude-sonnet-4-6")

    agent_a = client.as_agent(
        name="AgentA",
        instructions="You are a helpful assistant. Respond briefly.",
    )

    agent_b = client.as_agent(
        name="AgentB",
        instructions="You are a helpful assistant. Summarize what you received.",
    )

    # AgentA's assistant output flows directly to AgentB.
    # Anthropic rejects this because the conversation ends with an assistant message.
    workflow = (
        WorkflowBuilder(start_executor=agent_a)
        .add_edge(agent_a, agent_b)
        .build()
    )

    async for event in workflow.run("Say hello", stream=True):
        if isinstance(event, AgentResponseUpdate) and event.text:
            print(event.text, end="", flush=True)

    print()


if __name__ == "__main__":
    asyncio.run(main())

Error Messages / Stack Traces

anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'This model does not support assistant message prefill. The conversation must end with a user message.'}

Package Versions

agent-framework-rc6

Python Version

3.12.1

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpython

Type

No fields configured for Bug.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions