BerriAI / litellm

Call all LLM APIs using the OpenAI format. Use Bedrock, Azure, OpenAI, Cohere, Anthropic, Ollama, Sagemaker, HuggingFace, Replicate (100+ LLMs)
https://docs.litellm.ai/docs/
Other
10.05k stars 1.12k forks source link

[Bug]: typing of model_list passed to litellm.Router is bit off #4231

Open fingon opened 1 week ago

fingon commented 1 week ago

What happened?

I attempted to pass list[litellm.DeploymentTypedDict] to the litellm.Router as a parameter. That doesn't really work, unfortunately, as shown below.

I would perhaps rather type it as

list[DeploymentTypedDict] | list[dict[Any, Any]] | None

Relevant log output

...: error: Argument "model_list" to "Router" has incompatible type "list[DeploymentTypedDict]"; expected "list[DeploymentTypedDict | dict[Any, Any]] | None"  [arg-type]


### Twitter / LinkedIn details

_No response_
krrishdholakia commented 1 week ago

hey @fingon can you share your sample code for repro'ing the typing error?

fingon commented 1 week ago

Given this ,litellm.py:

import litellm

model_list: list[litellm.DeploymentTypedDict] = [
        {
            "model_name": "gpt-3.5-turbo",
            "litellm_params": {
                "model": "azure/gpt-35-turbo",
                "api_key": "x",
                "api_version": "y",
                "api_base": "z",
            },
        },
]

router = litellm.Router(model_list=model_list)

Shell action:

mstenber@koneko ~>python3 --version
Python 3.11.9
mstenber@koneko ~>mypy --version
mypy 1.10.0 (compiled: yes)
mstenber@koneko ~>mypy ,litellm.py 
,litellm.py:30: error: Argument "model_list" to "Router" has incompatible type "list[DeploymentTypedDict]"; expected "list[DeploymentTypedDict | dict[Any, Any]] | None"  [arg-type]
,litellm.py:30: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
,litellm.py:30: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)

Note that your typing should really use Sequence instead of list too, as it is covariant.