Skip to content

fix(llm): improve message handling to support LLMs without content/tool_calls#635

Merged
mannaandpoem merged 2 commits intoFoundationAgents:mainfrom
a-holm:avoid-Validation-error-when-using-gemini
Mar 15, 2025
Merged

fix(llm): improve message handling to support LLMs without content/tool_calls#635
mannaandpoem merged 2 commits intoFoundationAgents:mainfrom
a-holm:avoid-Validation-error-when-using-gemini

Conversation

@a-holm
Copy link
Copy Markdown
Contributor

@a-holm a-holm commented Mar 14, 2025

Features

  • Improved message handling in the LLM class to gracefully handle messages without content or tool_calls fields.
  • Reordered message processing to check for Message objects first.
  • Changed validation approach to silently skip malformed messages instead of raising a ValueError.
  • Removed strict validation for missing content or tool_calls fields.

This fixes:

Feature Docs
N/A (Minor change to improve compatibility with Google's Gemini LLM.)

Influence
This change ensures compatibility with LLMs like Google's Gemini, which may return messages without content or tool_calls fields once in a while, they always seem useless anyway. It improves robustness and prevents crashes when working with such models.

Result

  • Pre-commit checks passed after reformatting app/llm.py with black.
  • Tested with Google's Gemini and various message formats to confirm compatibility.

Other
This is a small but impactful change that unlocks support for Google's Gemini LLM while maintaining compatibility with existing models. And solves multiple issues.


Let me know if you’d like further adjustments!

…ol_calls

This commit improves the message handling in the LLM class to gracefully handle
messages without 'content' or 'tool_calls' fields. Previously, the system would
raise a ValueError when encountering such messages, causing crashes when working
with models like Google's Gemini that sometimes return messages with different
structures.

Key changes:
- Reordered message processing to check for Message objects first
- Changed validation approach to silently skip malformed messages instead of crashing
- Removed the strict ValueError when content/tool_calls are missing

This change maintains compatibility with correctly formatted messages while
improving robustness when working with various LLM providers.
@a-holm a-holm marked this pull request as draft March 14, 2025 20:44
@a-holm a-holm marked this pull request as ready for review March 14, 2025 20:47
@a-holm a-holm mentioned this pull request Mar 14, 2025
@mannaandpoem
Copy link
Copy Markdown
Collaborator

LGTM

@mannaandpoem
Copy link
Copy Markdown
Collaborator

Please run pre-commit to format code

@mannaandpoem
Copy link
Copy Markdown
Collaborator

format_messages this function name doesn't seem very good, it focuses on formatting ToolCallAgent messages

@a-holm
Copy link
Copy Markdown
Contributor Author

a-holm commented Mar 15, 2025

Ran pre-commit, I was so sure that I had already commited the changes, i even mentioned running pre-commit in the description. But now i pushed the changes.

format_messages this function name doesn't seem very good, it focuses on formatting ToolCallAgent messages

Perhaps, but I didn't make that change and it has been like that for a long time and it is a more involved and bigger task since we need to check if it breaks something somewhere since it has been the name for a while. Probably more suitable for another pull request perhaps?

@mannaandpoem
Copy link
Copy Markdown
Collaborator

That's Good! Thank you ~

@mannaandpoem mannaandpoem merged commit 337adf0 into FoundationAgents:main Mar 15, 2025
2 checks passed
@mannaandpoem
Copy link
Copy Markdown
Collaborator

mannaandpoem commented Mar 15, 2025

For ToolCallAgent, tool_choices is auto.If modified like this, tool_choices should modify to required.
Because auto allows message not to contain tool_calls

https://platform.openai.com/docs/guides/function-calling?api-mode=chat#additional-configurations

@a-holm
Copy link
Copy Markdown
Contributor Author

a-holm commented Mar 15, 2025

For ToolCallAgent, tool_choices is auto.If modified like this, tool_choices should modify to required. Because auto allows message not to contain tool_calls

https://platform.openai.com/docs/guides/function-calling?api-mode=chat#additional-configurations

My code should also allow message without tool_calls if they have a content. line 93 in llm.py says "content" in message or "tool_calls" in message:, I wrote "or" instead of "and"

@a-holm a-holm deleted the avoid-Validation-error-when-using-gemini branch March 15, 2025 09:25
@xraisen
Copy link
Copy Markdown

xraisen commented Apr 8, 2025

app.llm:ask_tool:763 - OpenAI API error: Error code: 400 - [{'error': {'code': 400, 'message': '* GenerateContentRequest.contents: contents is not specified\n', 'status': 'INVALID_ARGUMENT'}}]

iHeyTang pushed a commit to iHeyTang/OpenManus that referenced this pull request Apr 11, 2025
…error-when-using-gemini

fix(llm): improve message handling to support LLMs without content/tool_calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No tool content gemini api 运行报这个错误是什么原因呢?

3 participants