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

[Feature]: `function_to_dict` supporting defaulted arguments #4250

Open jamesbraza opened 1 week ago

jamesbraza commented 1 week ago

The Feature

As of litellm==1.40.15, default arguments are not supported within litellm.utils.function_to_dict. It would be great to support primitive defaults in this utility function.

For example, the below code will not properly include the default value:

import litellm

def foo(arg: int = 5) -> None:
    """
    Stub.

    Args:
        arg: Something defaulted to 5.
    """

litellm.utils.function_to_dict(foo)

To show how a type union is done in JSON schema, we can refer to pydantic==2.7.4:

from pydantic import BaseModel

class Foo(BaseModel):
    bar: int = 5

print(Foo.model_json_schema())

Will print (after prettifying):

{
   "properties":{
      "bar":{
         "default":5,
         "title":"Bar",
         "type":"integer"
      }
   },
   "title":"Foo",
   "type":"object"
}

Motivation, pitch

There are times when a function will contain a default argument.

Twitter / LinkedIn details

No response