Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Kludex merged 3 commits intopydantic:mainfromoscar-broman:patch-1
May 27, 2025

Conversation

oscar-broman
Copy link
Contributor

This PR fixes an issue where empty messages are created.

The error happens when the function_map_message runs with this input:

[    ModelRequest(parts=[SystemPromptPart(content='<my system prompt>', dynamic_ref=None, part_kind='system-prompt')], kind='request'),    ModelResponse(parts=[TextPart(content='<model response>', part_kind='text')], model_name=None, timestamp=datetime.datetime(2025, 3, 2, 7, 10, 12, 560121, tzinfo=datetime.timezone.utc), kind='response'),    ModelRequest(parts=[UserPromptPart(content="<user message>", timestamp=datetime.datetime(2025, 3, 2, 7, 10, 12, 566706, tzinfo=datetime.timezone.utc), part_kind='user-prompt')], kind='request'),]

The following error is eventually returned from the Anthropic API:

{'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.0: all messages must have non-empty content except for the optional final assistant message'}

zzstoatzz and mike-luabase reacted with thumbs up emoji
@Kludex
Copy link
Member

Can you provide an MRE, please?

@oscar-broman
Copy link
ContributorAuthor

fromanthropicimportAsyncAnthropicfromopenaiimportAsyncOpenAIfrompydantic_aiimportAgentfrompydantic_ai.messagesimportModelRequest,SystemPromptPart,TextPart,ModelResponse,UserPromptPartfrompydantic_ai.models.anthropicimportAnthropicModelfrompydantic_ai.models.openaiimportOpenAIModelasyncdeftest_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))),    ]forname,modelinmodels: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 uncommentedModelResponse(parts=[TextPart(content="HELLO")]),        ]awaitagent.run(user_prompt="Test",message_history=messages,result_type=str,        )

@alexmojaki
Copy link
Contributor

Please convert the MRE into a test

@oscar-broman
Copy link
ContributorAuthor

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?

@alexmojakialexmojaki requested a review fromKludexMarch 4, 2025 08:26
@alexmojaki
Copy link
Contributor

OK then I think it's fine unless@Kludex has some ideas.

@Kludex
Copy link
Member

I'll create a test with VCR.

@mike-luabase
Copy link
Contributor

@Kludex I'm hitting this error on most of our attempts to use Claude 3.7

@KludexKludex marked this pull request as ready for reviewMay 27, 2025 15:48
@KludexKludexenabled auto-merge (squash)May 27, 2025 15:48
@KludexKludex merged commit3b12530 intopydantic:mainMay 27, 2025
16 checks passed
Kludex added a commit that referenced this pull requestMay 30, 2025
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@KludexKludexKludex approved these changes

Assignees

@KludexKludex

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@oscar-broman@Kludex@alexmojaki@mike-luabase@DouweM

[8]ページ先頭

©2009-2025 Movatter.jp