Skip to main content

Universal wrapper for Python functions to be used with LLM function calling

Project description

Python wrapper for LLM function calling

Function calling provides a powerful and flexible way for Large Language Models (LLMs) such as OpenAI and Anthropic models to interface with external systems (e.g. execute actions on the user's behalf) and access data outside their training data (e.g. search an internal database).

This Python module provides facilities to wrap Python classes and functions such that they can be passed to the tools parameter when creating a model with the OpenAI Responses API or Claude API in Python. Specifically, it automatically generates a JSON schema from the function signature, doc-string and Pydantic Field definitions of parameters of BaseModel type, and unwraps/wraps input/output, filtering and propagating exceptions.

Features

  • generates JSON schema based on function signature
  • utilizes Field in parameters of types deriving from BaseModel
  • supports both standard and async Python functions
  • supports exposing all methods of a Python class eligible for function calling
  • marshals input/output between LLM model and Python function
  • catches and wraps exceptions

Example

Below you find a comprehensive example in which we

  • define a tool group with invocable functions
  • create an asynchronous event stream with OpenAI's Responses API
  • process the response stream to collect tool calls
  • invoke tools concurrently supplying collected arguments
  • transform tool inputs and outputs into messages that we can pass as inputs to subsequent Response API calls

Define a tool group

First, define a class that derives from FunctionToolGroup, and implement member functions that take no parameters, or a single parameter of type str, B or list[B] where B derives from ToolBaseModel:

class SearchQuery(ToolBaseModel):
    "Finds relevant documents in a database."

    phrase: str = Field(..., description="A search phrase that captures what the user is looking for.")


class SearchResultItem(ToolBaseModel):
    "A document in the database that matches the user's query."

    id: str = Field(..., description="Unique identifier for the document found in the database.")
    content: str = Field(..., description="Document text in Markdown format.")
    similarity: int = Field(..., description="Measures similarity to the user's query on a range from 0 (least similar) to 100 (most similar).")


class SearchToolGroup(FunctionToolGroup):
    "Finds relevant documents in a database."

    connection: object

    def __init__(self, connection: object) -> None:
        self.connection = connection

    async def find_documents(self, query: SearchQuery) -> list[SearchResultItem]:
        "Performs a search on the database to find documents that match the search phrase."

        sql = "SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ..."
        rows: list[dict[str, typing.Any]] = []
        rows.extend(await self.connection.execute(sql))  # type: ignore
        return [SearchResultItem(id=row["id"], content=row["content"], similarity=row["similarity"]) for row in rows]

Next, generate tools by calling tool_group.async_invocables() on the tool group, which discovers eligible functions:

# create function tool group
tool_group = SearchToolGroup(connection)

# create invocables
tools = tool_group.async_invocables()

Create a response stream

Pass the list of tools obtained from the tool group to create a response stream using OpenAI's Response API:

async def create_with_tools(
    client: AsyncOpenAI, prompt: str, messages: list[ResponseInputItemParam], tools: list[AsyncInvocable]
) -> AsyncStream[ResponseStreamEvent]:
    """
    Creates a model response stream, enabling a set of tools with function calling.

    :param client: Client proxy for GPT API.
    :param prompt: System prompt for LLM.
    :param messages: Prior messages in the conversation, including user and assistant messages, tool call requests and responses.
    :param tools: Tools to enable with function calling.
    :returns: An asynchronous stream of response events.
    """

    return await client.responses.create(
        stream=True,
        model="gpt-4o-mini",
        instructions=prompt,
        input=messages,
        store=False,
        tools=[
            FunctionToolParam(
                name=tool.name,
                # obtain tool description from function doc-string
                description=tool.description,
                # derive JSON schema from function signature
                parameters=typing.cast(dict[str, object], tool.input_schema()),
                strict=True,
                type="function",
            )
            for tool in tools
        ],
    )

Process response stream

Process the events in the response stream, registering any function calls that the GPT LLM requests to invoke:

async def process_response_stream(events: AsyncStream[ResponseStreamEvent]) -> list[ToolCall]:
    """
    Processes events in a response stream.

    :param events: An asynchronous stream of response events.
    """

    tool_refs: dict[str, ToolRef] = {}
    tool_calls: list[ToolCall] = []

    async for event in events:
        if isinstance(event, ResponseOutputItemAddedEvent):
            if isinstance(event.item, ResponseFunctionToolCall):
                # supplies the function name and a unique call identifier
                if event.item.id is not None:
                    tool_refs[event.item.id] = ToolRef(event.item.call_id, event.item.name)
        elif isinstance(event, ResponseFunctionCallArgumentsDoneEvent):
            # supplies the complete JSON string of function arguments
            tool_ref = tool_refs.pop(event.item_id)
            tool_calls.append(ToolCall(event.item_id, tool_ref.call_id, tool_ref.name, event.arguments))
        else:
            # process other types of events, including message deltas
            pass

    return tool_calls

Execute function call requests

Finally, execute the function call requests by the GPT LLM concurrently and feed back call output as input messages to the GPT LLM:

async def invoke_tools(tools: list[AsyncInvocable], tool_calls: list[ToolCall]) -> list[ResponseInputItemParam]:
    """
    Calls user-defined tools invoked by the LLM.

    :param tools: Tools enabled when creating the response.
    :param tool_calls: Tools invoked by the LLM in the latest response stream.
    :returns: Messages corresponding to tool call requests and responses.
    """

    tool_directory = {tool.name: tool for tool in tools}
    messages: list[ResponseInputItemParam] = []

    # invoke tools concurrently
    tasks: list[Task[str]] = []
    async with TaskGroup() as tg:
        for tool_call in tool_calls:
            tool = tool_directory[tool_call.name]
            tasks.append(tg.create_task(tool(tool_call.args)))

    for tool_call, task in zip(tool_calls, tasks, strict=True):
        call_result = task.result()

        messages.append(
            ResponseFunctionToolCallParam(
                id=tool_call.id,
                call_id=tool_call.call_id,
                name=tool_call.name,
                arguments=tool_call.args,
                type="function_call",
            )
        )
        messages.append(
            FunctionCallOutput(
                id=tool_call.id,
                call_id=tool_call.call_id,
                output=call_result,
                type="function_call_output",
            )
        )

    return messages

Implementation

ToolBaseModel configures how the JSON schema is generated by Pydantic such that additionalProperties are disallowed to ensure compliance with OpenAI's function tool calling convention in strict mode. Otherwise, ToolBaseModel is equivalent to a plain Pydantic BaseModel.

The implementation makes use of partial type erasure, erasing B (a sub-class of ToolBaseModel) to ToolBaseModel, and list[B] to list[ToolBaseModel]. This lowers the number of possible input/output combinations that need to be generated, yet allows functions such as model_json_schema and model_validate_json to be called in context as necessary. This lets us eagerly evaluate some expressions, and elide function calls.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_function_tool-0.1.3.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_function_tool-0.1.3-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file agent_function_tool-0.1.3.tar.gz.

File metadata

  • Download URL: agent_function_tool-0.1.3.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for agent_function_tool-0.1.3.tar.gz
Algorithm Hash digest
SHA256 004e9e385c668b445984b5a94fb17f3fb559f51ba8776c472a22b55f7555c69c
MD5 b59093dfcc19d0efe5eea7902d4dbe2f
BLAKE2b-256 d764dea6a8d9c0b9b6a3568f22426d953c304b06a30e93824daa63a0224e15ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_function_tool-0.1.3.tar.gz:

Publisher: publish-python.yml on hunyadi/function_tool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file agent_function_tool-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_function_tool-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3e5260042bf363ee017aa97a2a2735f990f074618041bc6e3cd2773950db2308
MD5 d4ced95355ba071682448b9af4b9bf66
BLAKE2b-256 747d8c4ba2ba47cbefeb3778fe5b5a362ac8d9a66a4c990de97a52715fd6eab8

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_function_tool-0.1.3-py3-none-any.whl:

Publisher: publish-python.yml on hunyadi/function_tool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page