Skip to main content

lightweight tool-calling runner for the OpenAI Responses API.

Project description

openai-resptools

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 ToolRegistry with a @registry.tool() decorator
  • Optional “registry as a class” pattern (public methods auto-registered)
  • ToolRunner that round-trips model output items back into input
  • Configurable max_iters, max_retry, parallel_tool_calls, and on_event callback

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


Download files

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

Source Distribution

openai_resptools-0.1.0.tar.gz (581.7 kB view details)

Uploaded Source

Built Distribution

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

openai_resptools-0.1.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

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

Hashes for openai_resptools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa26b20543fdded7907ff58e27f94f63d21a591275ce56ae950da4548c6b1b7c
MD5 7db25eb65cf609e27ea325502e4434c7
BLAKE2b-256 31ee7f4a44af2dff1f1b3b3169a13246f0e2b6064761da8eacb991f92efb2e27

See more details on using hashes here.

File details

Details for the file openai_resptools-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_resptools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc2ae7e22b0d6cc01a9ce652562cdf9390e2c7423cff3ebec05b81a671004979
MD5 4811204b5de24273baf21586fead7212
BLAKE2b-256 809c184a24d59f1172f2100a414a4508b904c229ff42629309ac25877ed86e06

See more details on using hashes here.

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