-
Notifications
You must be signed in to change notification settings - Fork 923
Fix bug where empty messages might be created in the Anthropic model #1027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Can you provide an MRE, please? |
from anthropic import AsyncAnthropic
from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.messages import ModelRequest, SystemPromptPart, TextPart, ModelResponse, UserPromptPart
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.models.openai import OpenAIModel
async def test_anthropic_issue() -> None:
openai_key = ""
anthropic_key = ""
models = [
("OpenAI", OpenAIModel(model_name="gpt-4o-mini", openai_client=AsyncOpenAI(api_key=openai_key))),
("Anthropic", AnthropicModel(model_name="claude-3-7-sonnet-latest", anthropic_client=AsyncAnthropic(api_key=anthropic_key))),
]
for name, model in models:
print(f"\n\nModel: {name}")
agent = Agent(
model=model,
)
messages = [
ModelRequest(parts=[SystemPromptPart("Respond with the user's last message in upper-case")]),
# ModelRequest(parts=[UserPromptPart(content="Hello")]), <-- Issue does not happen if uncommented
ModelResponse(parts=[TextPart(content="HELLO")]),
]
await agent.run(
user_prompt="Test",
message_history=messages,
result_type=str,
) |
Please convert the MRE into a test |
I've tried to do this without any other changes in the codebase -- it's quite hard. The function is an internal function not accessible without some indirections. One way would be to create a live test, but this seems a bit extreme perhaps. @alexmojaki What would you suggest I do? |
OK then I think it's fine unless @Kludex has some ideas. |
I'll create a test with VCR. |
@Kludex I'm hitting this error on most of our attempts to use Claude 3.7 |
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
This PR fixes an issue where empty messages are created.
The error happens when the function
_map_message
runs with this input:The following error is eventually returned from the Anthropic API: