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
Fieldin parameters of types deriving fromBaseModel - supports both standard and
asyncPython 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
004e9e385c668b445984b5a94fb17f3fb559f51ba8776c472a22b55f7555c69c
|
|
| MD5 |
b59093dfcc19d0efe5eea7902d4dbe2f
|
|
| BLAKE2b-256 |
d764dea6a8d9c0b9b6a3568f22426d953c304b06a30e93824daa63a0224e15ff
|
Provenance
The following attestation bundles were made for agent_function_tool-0.1.3.tar.gz:
Publisher:
publish-python.yml on hunyadi/function_tool
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_function_tool-0.1.3.tar.gz -
Subject digest:
004e9e385c668b445984b5a94fb17f3fb559f51ba8776c472a22b55f7555c69c - Sigstore transparency entry: 865988955
- Sigstore integration time:
-
Permalink:
hunyadi/function_tool@86f9350ed7585578d577b6d307919aeed33a8c1c -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/hunyadi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@86f9350ed7585578d577b6d307919aeed33a8c1c -
Trigger Event:
push
-
Statement type:
File details
Details for the file agent_function_tool-0.1.3-py3-none-any.whl.
File metadata
- Download URL: agent_function_tool-0.1.3-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e5260042bf363ee017aa97a2a2735f990f074618041bc6e3cd2773950db2308
|
|
| MD5 |
d4ced95355ba071682448b9af4b9bf66
|
|
| BLAKE2b-256 |
747d8c4ba2ba47cbefeb3778fe5b5a362ac8d9a66a4c990de97a52715fd6eab8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_function_tool-0.1.3-py3-none-any.whl -
Subject digest:
3e5260042bf363ee017aa97a2a2735f990f074618041bc6e3cd2773950db2308 - Sigstore transparency entry: 865989002
- Sigstore integration time:
-
Permalink:
hunyadi/function_tool@86f9350ed7585578d577b6d307919aeed33a8c1c -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/hunyadi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@86f9350ed7585578d577b6d307919aeed33a8c1c -
Trigger Event:
push
-
Statement type: