Skip to content
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

[Bug] Gemini Api 400 Invalid Argument Error #2241

Closed
AkhilmsAchu opened this issue Feb 27, 2025 · 7 comments · Fixed by #2270
Closed

[Bug] Gemini Api 400 Invalid Argument Error #2241

AkhilmsAchu opened this issue Feb 27, 2025 · 7 comments · Fixed by #2270
Labels
bug Something isn't working

Comments

@AkhilmsAchu
Copy link

AkhilmsAchu commented Feb 27, 2025

Description

This error just pops up very randomly.

Couldn't find function to call due to error from Gemini Api

Error from Gemini API: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Please use a valid role: user,
         model.', 'status': 'INVALID_ARGUMENT'}}

Steps to Reproduce

List the steps needed to encounter this bug or issue.

Expected Behavior

It should have called the right tool

Actual Behavior

Getting Exception and tool call failed

Screenshots or Logs (if applicable)

Sorry i had to trim logs due to privacy concerns😅
DEBUG    **************** METRICS ****************
DEBUG    * Input tokens:                772
DEBUG    * Output tokens:               28
DEBUG    * Total tokens:                800
DEBUG    * Time:                        2.4601s
DEBUG    * Tokens per second:           11.3815 tokens/s
DEBUG    **************** METRICS ******************
DEBUG    ============== user ==============
DEBUG    Could not find function to call.
DEBUG    ============== tool ==============
ERROR    Error from Gemini API: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Please use a valid role: user,
         model.', 'status': 'INVALID_ARGUMENT'}}
WARNING  Attempt 1/1 failed: <Response [400]>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.venv\Lib\site-packages\agno\agent\agent.py", line 885, in run
    raise Exception(
Exception: Failed after 1 attempts. Last error using Gemini(gemini-2.0-flash-001): <Response [400]>

Xxxxxxxxxx.venv\Lib\site-packages\agno\models\google\gemini.py", line 334, in invoke
    raise ModelProviderError(
agno.exceptions.ModelProviderError: <Response [400]>

Environment

  • OS: Windows 11
  • Agno Version: v1.1.7
  • Additional Environment Details: Python 3.12

Possible Solutions (optional)

Suggest any ideas you might have to fix or address the issue.

Additional Context

Master agent is having a slave agent with multiple tools.

@AkhilmsAchu AkhilmsAchu added the bug Something isn't working label Feb 27, 2025
@dirkbrnd
Copy link
Contributor

Hi @AkhilmsAchu
Are you possibly trying structured output with tool calling? We have seen that error in those cases. Their SDK does not yet support it, but should soon.
If that is not the issue, mind sharing your agent code so we can replicate?

@AkhilmsAchu
Copy link
Author

Hi @dirkbrnd ,
No I'm not using structured Output. Below is my agent code (masked) sry again😅
`slave = Agent(
        name="slave",
        model=gemini_model,
        tools=[tool1,tool2,tool3],
        markdown=True,
        add_history_to_messages=True,
        num_history_responses=3,
        debug_mode=True,
        storage=storage,
        session_id= session_id if session_id else None,
        role="xxxxxx",
        instructions=["xxxxxxxx"],
        description="""xxxxxxxx"""
    )

master = Agent(
        name="master",
        model=gemini_model,
        team=[slave],
        session_id= session_id if session_id else None,
        markdown=True,
        add_history_to_messages=True,
        num_history_responses=3,
        debug_mode=True,
        storage=storage,
        instructions=["xxxxxxx"],
        description="""xxxx"""
    )`

@AkhilmsAchu
Copy link
Author

These are my findings after debugging the Gemini request parameters.

`Content(parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text='Could not find function to call.')], role='user'),

Content(parts=[], role='tool')`

These are the last 2 elements in formatted_messages before gemini started throwing the error.

In gemini.py file, adding these lines to skip items with role = tool and not having tool_calls attributes (after line no : 432 in _format_messages fn) fixed the issue temporarily for me.

if message.role == "tool" and not message.tool_calls: continue

I'm not sure whether this a proper fix. Maybe this will give you a hint to the real problem.

AkhilmsAchu added a commit to AkhilmsAchu/agno that referenced this issue Feb 28, 2025
Skip messages with role 'tool' and no tool_calls attribute while formating messages for gemini

This apparently fixes the error agno-agi#2241
@dirkbrnd
Copy link
Contributor

dirkbrnd commented Mar 3, 2025

@AkhilmsAchu the issue comes where Gemini makes an invalid tool call, we should just let it know that, but instead we send invalid payload.
So your fix might be correct.

@dirkbrnd
Copy link
Contributor

dirkbrnd commented Mar 3, 2025

I found an alternative way to fix, I'll post a PR

@dirkbrnd
Copy link
Contributor

dirkbrnd commented Mar 3, 2025

#2270

@AkhilmsAchu
Copy link
Author

@dirkbrnd thankyou for addressing the issue. Hope it will be released soon 🤞

dirkbrnd added a commit that referenced this issue Mar 3, 2025
## Description

There is a breaking issue when Gemini makes an incorrect function call.

Fixes #2241

---

## Type of change

Please check the options that are relevant:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Model update (Addition or modification of models)
- [ ] Other (please describe):

---

## Checklist

- [ ] Adherence to standards: Code complies with Agno’s style guidelines
and best practices.
- [ ] Formatting and validation: You have run `./scripts/format.sh` and
`./scripts/validate.sh` to ensure code is formatted and linted.
- [ ] Self-review completed: A thorough review has been performed by the
contributor(s).
- [ ] Documentation: Docstrings and comments have been added or updated
for any complex logic.
- [ ] Examples and guides: Relevant cookbook examples have been included
or updated (if applicable).
- [ ] Tested in a clean environment: Changes have been tested in a clean
environment to confirm expected behavior.
- [ ] Tests (optional): Tests have been added or updated to cover any
new or changed functionality.

---

## Additional Notes

Include any deployment notes, performance implications, security
considerations, or other relevant information (e.g., screenshots or logs
if applicable).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants