lightweight tool-calling runner for the OpenAI Responses API.
Project description
openai-resptools
Lightweight tool-calling runner for the OpenAI Responses API.
What it is
openai-resptools is a small Python helper library that:
- Registers Python callables as OpenAI function tools
- Generates OpenAI-compatible tool schemas from Python signatures
- Runs the Responses API tool-calling loop until the model returns final text
Features
- Simple
ToolRegistrywith a@registry.tool()decorator - Optional “registry as a class” pattern (public methods auto-registered)
ToolRunnerthat round-trips model output items back into input- Configurable
max_iters,max_retry,parallel_tool_calls, andon_eventcallback
Install
pip install openai-resptools
Quickstart
Set your API key:
export OPENAI_API_KEY="..."
Then run a tool-calling loop:
import os
from typing import Dict
from openai import OpenAI
from openai_resptools import ToolRegistry, ToolRunner
store: Dict[str, str] = {}
registry = ToolRegistry()
@registry.tool()
def write(key: str, text: str) -> dict:
"""Save text under a key and return a small status payload."""
store[key] = text
return {"ok": True, "key": key, "length": len(text)}
@registry.tool()
def read(key: str) -> dict:
"""Read text by key. Returns null text if missing."""
return {"key": key, "text": store.get(key)}
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
model = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
runner = ToolRunner(
client,
model=model,
registry=registry,
max_iters=6,
parallel_tool_calls=False,
)
prompt = (
"Do the following steps using tools:\n"
"1) Call write to save the text 'Hello from tools!' under key 'greeting'.\n"
"2) Call read for key 'greeting'.\n"
"3) Reply with ONLY the value of the text you read.\n"
)
result = runner.run(prompt)
print(result.text)
Patterns
Registry as a class
Subclass ToolRegistry and expose public methods as tools:
from openai_resptools import ToolRegistry
class MyServiceTools(ToolRegistry):
def __init__(self, db_conn: str) -> None:
super().__init__()
self.db_conn = db_conn
def get_user_status(self, user_id: int) -> str:
return f"User {user_id} is active (DB: {self.db_conn})"
service = MyServiceTools(db_conn="postgres://localhost:5432")
print(service.names())
Manual dispatch (no runner)
You can generate tool schemas and call tools yourself:
import json
from openai_resptools import ToolRegistry
registry = ToolRegistry()
@registry.tool()
def calculator(a: int, b: int, op: str) -> int:
if op == "+":
return a + b
if op == "-":
return a - b
raise ValueError(f"Unsupported operation: {op!r}")
tools_payload = registry.as_openai_tools()
mock_call = {"name": "calculator", "arguments": '{"a": 10, "b": 5, "op": "+"}'}
args = json.loads(mock_call["arguments"])
output = registry.get(mock_call["name"]).call(args)
print(output)
Development
pip install -e ".[dev]"
pytest
ruff check .
License
Apache 2.0 License. See LICENSE.
Project details
Release history Release notifications | RSS feed
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 openai_resptools-0.1.0.tar.gz.
File metadata
- Download URL: openai_resptools-0.1.0.tar.gz
- Upload date:
- Size: 581.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa26b20543fdded7907ff58e27f94f63d21a591275ce56ae950da4548c6b1b7c
|
|
| MD5 |
7db25eb65cf609e27ea325502e4434c7
|
|
| BLAKE2b-256 |
31ee7f4a44af2dff1f1b3b3169a13246f0e2b6064761da8eacb991f92efb2e27
|
File details
Details for the file openai_resptools-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openai_resptools-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc2ae7e22b0d6cc01a9ce652562cdf9390e2c7423cff3ebec05b81a671004979
|
|
| MD5 |
4811204b5de24273baf21586fead7212
|
|
| BLAKE2b-256 |
809c184a24d59f1172f2100a414a4508b904c229ff42629309ac25877ed86e06
|