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.
Metadata
Release files for agent-function-tool 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agent_function_tool-0.1.3.tar.gz | 15.5 kB | With provenance | Details for agent_function_tool-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agent_function_tool-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for agent_function_tool-0.1.3-py3-none-any.whl |
Total release size: 28.1 kB
Release files / agent_function_tool-0.1.3.tar.gz
| Download URL | agent_function_tool-0.1.3.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
004e9e385c668b445984b5a94fb17f3fb559f51ba8776c472a22b55f7555c69c
|
|
BLAKE2b-256 checksum How to use checksums |
d764dea6a8d9c0b9b6a3568f22426d953c304b06a30e93824daa63a0224e15ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 27, 2026.
Transparency logRelease files / agent_function_tool-0.1.3-py3-none-any.whl
| Download URL | agent_function_tool-0.1.3-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e5260042bf363ee017aa97a2a2735f990f074618041bc6e3cd2773950db2308
|
|
BLAKE2b-256 checksum How to use checksums |
747d8c4ba2ba47cbefeb3778fe5b5a362ac8d9a66a4c990de97a52715fd6eab8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 27, 2026.
Transparency logSecurity for agent-function-tool 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for agent_function_tool-0.1.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.