funcall


Namefuncall JSON
Version 0.10.0 PyPI version JSON
download
home_pageNone
SummaryA toolkit for function calling, tool registration, and LiteLLM & OpenAI compatible tool integration with sync/async support.
upload_time2025-08-04 09:35:19
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords ai function call openai pydantic rich tooling
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Funcall

**Don't repeat yourself!**

Funcall is a Python library that simplifies the use of function calling, especially with OpenAI and Pydantic. Its core goal is to eliminate repetitive schema definitions and boilerplate, letting you focus on your logic instead of writing the same code again and again.

## Motivation

If you use only the OpenAI SDK, enabling function call support requires you to:

- Write your function logic
- Manually define a schema for each function
- Pass the schema through the `tools` parameter
- Parse and handle the function call results yourself

This process is repetitive and error-prone. Funcall automates schema generation and function call handling, so you only need to define your function once.

## Features

- Automatically generate schemas from your function signatures and Pydantic models
- Integrate easily with OpenAI's function calling API
- No more manual, repetitive schema definitions—just define your function once
- **Dynamic tool registration** - Add tools without defining actual functions
- Easy to extend and use in your own projects

## Installation

```bash
pip install funcall
```

## Usage Example

```python
import openai
from openai.types.responses import ResponseFunctionToolCall
from pydantic import BaseModel, Field
from funcall import Funcall

# Define your data model once
class AddForm(BaseModel):
    a: float = Field(description="The first number")
    b: float = Field(description="The second number")

# Define your function once, no need to repeat schema elsewhere
def add(data: AddForm) -> float:
    """Calculate the sum of two numbers"""
    return data.a + data.b

fc = Funcall([add])

resp = openai.responses.create(
    model="gpt-4.1",
    input="Use function call to calculate the sum of 114 and 514",
    tools=fc.get_tools(), # Automatically generates the schema
)

for o in resp.output:
    if isinstance(o, ResponseFunctionToolCall):
        result = fc.handle_function_call(o) # Automatically handles the function call
        print(result) # 628.0
```

Funcall can read the type hints and docstrings to generate the schema automatically:

```json
[
    {
        "type": "function",
        "name": "add",
        "description": "Calculate the sum of two numbers",
        "parameters": {
            "type": "object",
            "properties": {
                "a": {
                    "type": "number",
                    "description": "The first number"
                },
                "b": {
                    "type": "number",
                    "description": "The second number"
                }
            },
            "required": ["a", "b"],
            "additionalProperties": false
        },
        "strict": true
    }
]
```

## Dynamic Tools

Funcall now supports dynamic tool registration, allowing you to add tools without defining actual functions. This provides more flexibility in tool management.

### Adding Dynamic Tools

Use the `add_dynamic_tool` method to register tools directly through metadata:

```python
from funcall import Funcall

funcall = Funcall()

# Add a basic tool
funcall.add_dynamic_tool(
    name="calculator",
    description="Perform basic mathematical operations",
    parameters={
        "operation": {
            "type": "string",
            "description": "The operation to perform",
            "enum": ["add", "subtract", "multiply", "divide"]
        },
        "a": {
            "type": "number", 
            "description": "The first number"
        },
        "b": {
            "type": "number",
            "description": "The second number"
        }
    },
    required=["operation", "a", "b"],
    handler=lambda operation, a, b: {
        "add": a + b,
        "subtract": a - b, 
        "multiply": a * b,
        "divide": a / b if b != 0 else "Cannot divide by zero"
    }[operation]
)
```

### Parameters

- `name`: Tool name
- `description`: Tool description  
- `parameters`: Parameter definitions (JSON Schema format)
- `required`: List of required parameter names (optional)
- `handler`: Custom handler function (optional)

### Handler Options

#### With Custom Handler

```python
def custom_handler(city: str, units: str = "celsius") -> dict:
    return {"city": city, "temperature": "25°C", "units": units}

funcall.add_dynamic_tool(
    name="get_weather",
    description="Get weather information",
    parameters={
        "city": {"type": "string", "description": "City name"},
        "units": {"type": "string", "description": "Temperature units", "default": "celsius"}
    },
    required=["city"],
    handler=custom_handler
)
```

#### Without Handler (Default Behavior)

If no handler is provided, the tool returns call information:

```python
funcall.add_dynamic_tool(
    name="simple_tool",
    description="A simple tool",
    parameters={
        "input": {"type": "string", "description": "Input parameter"}
    },
    required=["input"]
)

# When called, returns:
# {
#     "tool": "simple_tool",
#     "arguments": {"input": "value"},
#     "message": "Tool 'simple_tool' called with arguments: {'input': 'value'}"
# }
```

### Removing Dynamic Tools

```python
funcall.remove_dynamic_tool("tool_name")
```

### Integration with Existing Features

Dynamic tools are fully integrated with existing functionality:

- `get_tools()` - Includes dynamic tool definitions
- `call_function()` / `call_function_async()` - Call dynamic tools
- `handle_function_call()` - Handle LLM tool calls
- `get_tool_meta()` - Get tool metadata

### Use Cases

1. **Rapid Prototyping** - Test tools without defining functions
2. **Configuration-Driven Tools** - Create tools dynamically from config files
3. **API Proxy Tools** - Create tools that call external APIs
4. **Mocking and Testing** - Create mock tools for testing

See the `examples/` directory for more usage examples.

## License

MIT

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "funcall",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "AI, function call, openai, pydantic, rich, tooling",
    "author": null,
    "author_email": "Jianqi Pan <jannchie@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/9e/5e/e8a7aa156b74a9e769826c8bf030bd0a1c75587f2751b371995d4ca934e1/funcall-0.10.0.tar.gz",
    "platform": null,
    "description": "# Funcall\n\n**Don't repeat yourself!**\n\nFuncall is a Python library that simplifies the use of function calling, especially with OpenAI and Pydantic. Its core goal is to eliminate repetitive schema definitions and boilerplate, letting you focus on your logic instead of writing the same code again and again.\n\n## Motivation\n\nIf you use only the OpenAI SDK, enabling function call support requires you to:\n\n- Write your function logic\n- Manually define a schema for each function\n- Pass the schema through the `tools` parameter\n- Parse and handle the function call results yourself\n\nThis process is repetitive and error-prone. Funcall automates schema generation and function call handling, so you only need to define your function once.\n\n## Features\n\n- Automatically generate schemas from your function signatures and Pydantic models\n- Integrate easily with OpenAI's function calling API\n- No more manual, repetitive schema definitions\u2014just define your function once\n- **Dynamic tool registration** - Add tools without defining actual functions\n- Easy to extend and use in your own projects\n\n## Installation\n\n```bash\npip install funcall\n```\n\n## Usage Example\n\n```python\nimport openai\nfrom openai.types.responses import ResponseFunctionToolCall\nfrom pydantic import BaseModel, Field\nfrom funcall import Funcall\n\n# Define your data model once\nclass AddForm(BaseModel):\n    a: float = Field(description=\"The first number\")\n    b: float = Field(description=\"The second number\")\n\n# Define your function once, no need to repeat schema elsewhere\ndef add(data: AddForm) -> float:\n    \"\"\"Calculate the sum of two numbers\"\"\"\n    return data.a + data.b\n\nfc = Funcall([add])\n\nresp = openai.responses.create(\n    model=\"gpt-4.1\",\n    input=\"Use function call to calculate the sum of 114 and 514\",\n    tools=fc.get_tools(), # Automatically generates the schema\n)\n\nfor o in resp.output:\n    if isinstance(o, ResponseFunctionToolCall):\n        result = fc.handle_function_call(o) # Automatically handles the function call\n        print(result) # 628.0\n```\n\nFuncall can read the type hints and docstrings to generate the schema automatically:\n\n```json\n[\n    {\n        \"type\": \"function\",\n        \"name\": \"add\",\n        \"description\": \"Calculate the sum of two numbers\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"a\": {\n                    \"type\": \"number\",\n                    \"description\": \"The first number\"\n                },\n                \"b\": {\n                    \"type\": \"number\",\n                    \"description\": \"The second number\"\n                }\n            },\n            \"required\": [\"a\", \"b\"],\n            \"additionalProperties\": false\n        },\n        \"strict\": true\n    }\n]\n```\n\n## Dynamic Tools\n\nFuncall now supports dynamic tool registration, allowing you to add tools without defining actual functions. This provides more flexibility in tool management.\n\n### Adding Dynamic Tools\n\nUse the `add_dynamic_tool` method to register tools directly through metadata:\n\n```python\nfrom funcall import Funcall\n\nfuncall = Funcall()\n\n# Add a basic tool\nfuncall.add_dynamic_tool(\n    name=\"calculator\",\n    description=\"Perform basic mathematical operations\",\n    parameters={\n        \"operation\": {\n            \"type\": \"string\",\n            \"description\": \"The operation to perform\",\n            \"enum\": [\"add\", \"subtract\", \"multiply\", \"divide\"]\n        },\n        \"a\": {\n            \"type\": \"number\", \n            \"description\": \"The first number\"\n        },\n        \"b\": {\n            \"type\": \"number\",\n            \"description\": \"The second number\"\n        }\n    },\n    required=[\"operation\", \"a\", \"b\"],\n    handler=lambda operation, a, b: {\n        \"add\": a + b,\n        \"subtract\": a - b, \n        \"multiply\": a * b,\n        \"divide\": a / b if b != 0 else \"Cannot divide by zero\"\n    }[operation]\n)\n```\n\n### Parameters\n\n- `name`: Tool name\n- `description`: Tool description  \n- `parameters`: Parameter definitions (JSON Schema format)\n- `required`: List of required parameter names (optional)\n- `handler`: Custom handler function (optional)\n\n### Handler Options\n\n#### With Custom Handler\n\n```python\ndef custom_handler(city: str, units: str = \"celsius\") -> dict:\n    return {\"city\": city, \"temperature\": \"25\u00b0C\", \"units\": units}\n\nfuncall.add_dynamic_tool(\n    name=\"get_weather\",\n    description=\"Get weather information\",\n    parameters={\n        \"city\": {\"type\": \"string\", \"description\": \"City name\"},\n        \"units\": {\"type\": \"string\", \"description\": \"Temperature units\", \"default\": \"celsius\"}\n    },\n    required=[\"city\"],\n    handler=custom_handler\n)\n```\n\n#### Without Handler (Default Behavior)\n\nIf no handler is provided, the tool returns call information:\n\n```python\nfuncall.add_dynamic_tool(\n    name=\"simple_tool\",\n    description=\"A simple tool\",\n    parameters={\n        \"input\": {\"type\": \"string\", \"description\": \"Input parameter\"}\n    },\n    required=[\"input\"]\n)\n\n# When called, returns:\n# {\n#     \"tool\": \"simple_tool\",\n#     \"arguments\": {\"input\": \"value\"},\n#     \"message\": \"Tool 'simple_tool' called with arguments: {'input': 'value'}\"\n# }\n```\n\n### Removing Dynamic Tools\n\n```python\nfuncall.remove_dynamic_tool(\"tool_name\")\n```\n\n### Integration with Existing Features\n\nDynamic tools are fully integrated with existing functionality:\n\n- `get_tools()` - Includes dynamic tool definitions\n- `call_function()` / `call_function_async()` - Call dynamic tools\n- `handle_function_call()` - Handle LLM tool calls\n- `get_tool_meta()` - Get tool metadata\n\n### Use Cases\n\n1. **Rapid Prototyping** - Test tools without defining functions\n2. **Configuration-Driven Tools** - Create tools dynamically from config files\n3. **API Proxy Tools** - Create tools that call external APIs\n4. **Mocking and Testing** - Create mock tools for testing\n\nSee the `examples/` directory for more usage examples.\n\n## License\n\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A toolkit for function calling, tool registration, and LiteLLM & OpenAI compatible tool integration with sync/async support.",
    "version": "0.10.0",
    "project_urls": null,
    "split_keywords": [
        "ai",
        " function call",
        " openai",
        " pydantic",
        " rich",
        " tooling"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "262675bef8165b8ee0374ffeed810036b9a6758405fe91a322aaae205d4f3a8c",
                "md5": "ebe635a5210ae5a6094e03a72c48bba8",
                "sha256": "73183f224495594452012643d1bd40937c00abf01dc5806e10dc715c1822d9cc"
            },
            "downloads": -1,
            "filename": "funcall-0.10.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ebe635a5210ae5a6094e03a72c48bba8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 13830,
            "upload_time": "2025-08-04T09:35:17",
            "upload_time_iso_8601": "2025-08-04T09:35:17.641238Z",
            "url": "https://files.pythonhosted.org/packages/26/26/75bef8165b8ee0374ffeed810036b9a6758405fe91a322aaae205d4f3a8c/funcall-0.10.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9e5ee8a7aa156b74a9e769826c8bf030bd0a1c75587f2751b371995d4ca934e1",
                "md5": "ba94c3f2f88c78da92a6efb9aac00599",
                "sha256": "4a6de6d7055ac033204f8aae3a0e26688bdae6ca5a9a59fee9d313cc75617de9"
            },
            "downloads": -1,
            "filename": "funcall-0.10.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ba94c3f2f88c78da92a6efb9aac00599",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 193769,
            "upload_time": "2025-08-04T09:35:19",
            "upload_time_iso_8601": "2025-08-04T09:35:19.842145Z",
            "url": "https://files.pythonhosted.org/packages/9e/5e/e8a7aa156b74a9e769826c8bf030bd0a1c75587f2751b371995d4ca934e1/funcall-0.10.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-04 09:35:19",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "funcall"
}
        
Elapsed time: 1.17005s