-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fix exception handling gemini #3493
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
litellm/utils.py
Outdated
from .integrations.prometheus import PrometheusLogger | ||
from .integrations.prometheus_services import PrometheusServicesLogger | ||
from .integrations.dynamodb import DyanmoDBLogger | ||
from .integrations.prompt_layer import PromptLayerLogger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason why so many imports in here+ init.py are refactored?
It makes it hard to isolate the actual changes you're trying to make in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is just changed by isort.
If you not want to aplly, I will drop "style(utils.py): apply formatter" commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please. it would help isolate the changes in this PR
Thank you for your work on this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dropped it.
Sorry, my commit include it.
Wait a moment please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the many imports change.
@@ -4602,6 +4606,9 @@ def register_model(model_cost: Union[str, dict]): | |||
elif value.get("litellm_provider") == "bedrock": | |||
if key not in litellm.bedrock_models: | |||
litellm.bedrock_models.append(key) | |||
elif value.get("litellm_provider") == "gemini": | |||
if key not in litellm.gemini_models: | |||
litellm.gemini_models.append(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed? All gemini models start with gemini/
and so already work without changes.
Can you share an example of the problem you're trying to solve with this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my invessgation, this code calls model without "gemini/" in case some Exception.
This is stack trace.
09:26:06 - LiteLLM:ERROR: utils.py:5873 - Error occurred in getting api base - GetLLMProvider Exception - name 'get_provider_from_model' is not defined
original model: gemini-1.5-pro-latest
fixed_litellm_completions second_error e2= 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
Error in chat: 429 Resource has been exhausted (e.g. check quota).
Traceback (most recent call last):
File "/app/litellm/litellm/llms/gemini.py", line 213, in completion
response = _model.generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/generativeai/generative_models.py", line 256, in generate_content
iterator = self._client.stream_generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 1090, in stream_generate_content
response = rpc(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
_retry_error_helper(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
raise final_exc from source_exc
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
result = target()
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/main.py", line 1684, in completion
model_response = gemini.completion(
File "/app/litellm/litellm/llms/gemini.py", line 241, in completion
raise GeminiError(
litellm.llms.gemini.GeminiError: 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm confused - yes, we make calls to gemini without the gemini/
as that is used by litellm as internal logic for routing.
Where is the get_llm_provider
error in this stack trace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, "LiteLLM:ERROR: utils.py:5873 - Error occurred in getting api base " shows the exception from get_llm_provider
here (line number is little changed by adding log).
This is stack trace from get_llm_provider().
It looks some times Exception handled.
I think the Exceition should be handked more early step.
This PR can fix "LiteLLM:ERROR" in get_llm_provider().
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/utils.py", line 6391, in get_llm_provider
raise litellm.exceptions.BadRequestError( # type: ignore
litellm.exceptions.BadRequestError: LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is full stack trace I get for one ERROR.
The "LiteLLM:ERROR" is show 2 times in real.
This behaivior was a little noisy to debug for me.
params_dump= {'model': 'gemini/gemini-1.5-pro-latest', 'messages': [{'role': 'system', 'content': 'You are Open Interpr'}, {'role': user, 'content': 'pythonで円周率を表示するプログラム'}, {'role': assistant, 'content': ''}, {'role': user, 'content': '会話履歴で状況を確認してから自動的に処理'}], 'stream': True, 'max_tokens': 4096}
Traceback (most recent call last):
File "/app/litellm/litellm/llms/gemini.py", line 213, in completion
response = _model.generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/generativeai/generative_models.py", line 256, in generate_content
iterator = self._client.stream_generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 1090, in stream_generate_content
response = rpc(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
_retry_error_helper(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
raise final_exc from source_exc
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
result = target()
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.ResourceExhausted: 429 Resource has been exhausted (e.g. check quota).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/main.py", line 1684, in completion
model_response = gemini.completion(
File "/app/litellm/litellm/llms/gemini.py", line 241, in completion
raise GeminiError(
litellm.llms.gemini.GeminiError: 429 Resource has been exhausted (e.g. check quota).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/utils.py", line 6391, in get_llm_provider
raise litellm.exceptions.BadRequestError( # type: ignore
litellm.exceptions.BadRequestError: LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest
Pass model as E.g. For 'Huggingface' inference endpoints pass in `completion(model='huggingface/starcoder',..)` Learn more: https://docs.litellm.ai/docs/providers
12:07:08 - LiteLLM:ERROR: utils.py:5870 - Error occurred in getting api base - LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest
Pass model as E.g. For 'Huggingface' inference endpoints pass in `completion(model='huggingface/starcoder',..)` Learn more: https://docs.litellm.ai/docs/providers
fixed_litellm_completions the first_error e= 429 Resource has been exhausted (e.g. check quota).
Traceback (most recent call last):
File "/app/litellm/litellm/llms/gemini.py", line 213, in completion
response = _model.generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/generativeai/generative_models.py", line 256, in generate_content
iterator = self._client.stream_generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 1090, in stream_generate_content
response = rpc(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
_retry_error_helper(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
raise final_exc from source_exc
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
result = target()
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/main.py", line 1684, in completion
model_response = gemini.completion(
File "/app/litellm/litellm/llms/gemini.py", line 241, in completion
raise GeminiError(
litellm.llms.gemini.GeminiError: 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/litellm/litellm/utils.py", line 6391, in get_llm_provider
raise litellm.exceptions.BadRequestError( # type: ignore
litellm.exceptions.BadRequestError: LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest
Pass model as E.g. For 'Huggingface' inference endpoints pass in `completion(model='huggingface/starcoder',..)` Learn more: https://docs.litellm.ai/docs/providers
12:07:08 - LiteLLM:ERROR: utils.py:5870 - Error occurred in getting api base - LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest
Pass model as E.g. For 'Huggingface' inference endpoints pass in `completion(model='huggingface/starcoder',..)` Learn more: https://docs.litellm.ai/docs/providers
fixed_litellm_completions second_error e2= 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
Error in chat: 429 Resource has been exhausted (e.g. check quota).
Traceback (most recent call last):
File "/app/litellm/litellm/llms/gemini.py", line 213, in completion
response = _model.generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/generativeai/generative_models.py", line 256, in generate_content
iterator = self._client.stream_generate_content(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 1090, in stream_generate_content
response = rpc(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
_retry_error_helper(
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
raise final_exc from source_exc
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
result = target()
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "/home/jinno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 API key not valid. Please pass a valid API key. [reason: "API_KEY_INVALID"
domain: "googleapis.com"
metadata {
key: "service"
value: "generativelanguage.googleapis.com"
}
]
0cd7d64
to
16fa8a7
Compare
16fa8a7
to
47e98cc
Compare
@nobu007 can you share a sample code snippet to repro the issue, this PR tries to solve? |
@krrishdholakia
This is result of my environment. (gamebook) jinno@jinno-desktop:~/git/drill/gamebook$ python test/test_litellm.py
Traceback (most recent call last):
File "/home/jinno/git/drill/gamebook/test/test_litellm.py", line 10, in <module>
{"role": user, "content": "pythonで円周率を表示するプログラム"},
NameError: name 'user' is not defined
(gamebook) jinno@jinno-desktop:~/git/drill/gamebook$ python test/test_litellm.py
i= 0
i= 1
i= 2
i= 3
i= 4
i= 5
Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new
LiteLLM.Info: If you need to debug this error, use `litellm.set_verbose=True'.
Provider List: https://docs.litellm.ai/docs/providers
Traceback (most recent call last):
File "/home/jinno/git/drill/gamebook/open_interpreter_docker/litellm/litellm/llms/gemini.py", line 213, in completion
response = _model.generate_content(
・・・
litellm.exceptions.APIConnectionError: 429 Resource has been exhausted (e.g. check quota). |
Please use for testing "gemini/gemini-1.5-pro-latest". It is easy to repro 429 error. |
Your error isn't being caused by litellm though - that just looks like you ran out of your vertex ai quota. What is the litellm error here? |
@krrishdholakia "LiteLLM:ERROR: utils.py:5870 - Error occurred in getting api base - LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest" "litellm.exceptions.BadRequestError: LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=gemini-1.5-pro-latest" |
Fix exception handling gemini
Add get_provider_from_model() for handling 429 Exception from gemini.
Relevant issues
This error is the target problem to resolved in PR.
From some case, model is not include llm_provider name.
It causes ERROR in get_llm_provider().
"""
09:26:06 - LiteLLM:ERROR: utils.py:5873 - Error occurred in getting api base - GetLLMProvider Exception - name 'get_provider_from_model' is not defined
original model: gemini-1.5-pro-latest
"""
Type
🧹 Refactoring
Changes
Testing
Works fine with this condition.
Finaly
Notes
Retlated with #3473.
This PR may help to reproduce the condition.
"gemini/gemini-1.5-pro-latest" soon 429 exception.
Pre-Submission Checklist (optional but appreciated):
OS Tests (optional but appreciated):