Skip to main content

Convert OpenAI-style tools and tool history into tagged text prompts, and tagged text back into OpenAI-style tool-call semantics.

Project description

toolcall-gateway

toolcall-gateway is a small Python library for one specific job:

  • tool2text: convert OpenAI-style tools and tool history into a strict tagged prompt for text-only models
  • text2tool: parse tagged model output back into OpenAI-style tool-call semantics

This project does not provide an HTTP API layer. You bring your own middleware, gateway, or runtime.

It also understands tool_choice semantics on both sides:

  • tool2text: writes auto / required / none / specific function constraints into the tagged prompt
  • text2tool: can validate the model output against the same constraints while parsing

Production Notes

  • The public API raises stable exceptions from toolcall_gateway.errors:
    • ToolcallGatewayError
    • TaggedOutputError
    • ToolChoiceError
  • The package ships a py.typed marker for static type checkers.
  • The repository includes runnable demos, tests, and a buildable wheel/sdist.

Tagged Protocol

The library uses a strict XML-like DSL:

<think>...</think>
<tool_calls>[{"name":"Read","arguments":{"path":"a.py"}}]</tool_calls>

or:

<think>...</think>
<final_answer>...</final_answer>

Install

uv pip install -e .

Example

from toolcall_gateway import build_prompt, parse_to_openai_assistant_turn

messages = [
    {"role": "user", "content": "Read a.py and summarize it."},
]
tools = [
    {
        "type": "function",
        "function": {
            "name": "Read",
            "description": "Read a file",
            "parameters": {
                "type": "object",
                "properties": {"path": {"type": "string"}},
                "required": ["path"],
            },
        },
    }
]

prompt = build_prompt(
    messages,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "Read"}},
)

# Send prompt to your own text-only model runtime...
model_output = (
    "<think>I need the file first</think>"
    '<tool_calls>[{"name":"Read","arguments":{"path":"a.py"}}]</tool_calls>'
)

turn = parse_to_openai_assistant_turn(model_output)
print(turn.finish_reason)  # tool_calls
print(turn.tool_calls[0].function.name)  # Read

Error Handling

from toolcall_gateway import ToolChoiceError, TaggedOutputError

try:
    turn = parse_to_openai_assistant_turn(
        model_output,
        tool_choice={"type": "function", "function": {"name": "Read"}},
    )
except ToolChoiceError:
    # The model violated the requested tool_choice constraint.
    ...
except TaggedOutputError:
    # The model produced invalid tagged text.
    ...

Demos

uv run python examples/demo_tool2text.py
uv run python examples/demo_text2tool.py

Verification

make verify

or:

uv run --with ruff ruff check .
uv run python -m unittest discover -s tests
uv build

Tests

uv run python -m unittest discover -s tests

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

toolcall_gateway-1.0.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

toolcall_gateway-1.0.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file toolcall_gateway-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for toolcall_gateway-1.0.0.tar.gz
Algorithm Hash digest
SHA256 384f27c959d39b6dde70788602d7d0bf27fd71283bd41027b637847d0696c5b7
MD5 f471e8f39dc78519685dbb44f22148b9
BLAKE2b-256 7337789955a5479808ce0aad0e0c2e52b653a1dfcdf550d35cc66b6283c89666

See more details on using hashes here.

Provenance

The following attestation bundles were made for toolcall_gateway-1.0.0.tar.gz:

Publisher: ci.yml on caiwuu/toolcall-gateway

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

File details

Details for the file toolcall_gateway-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for toolcall_gateway-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82b5fdc3bd2b60518b81cedd7c2d5f88787fa64fb44be06f7c24698c1064ca8b
MD5 0e043affd8d01da3b9d75fa42c917097
BLAKE2b-256 3258a0072dddb2acb7a149d5b64f964e6bae09b0a155c44802a9eb06c77a6210

See more details on using hashes here.

Provenance

The following attestation bundles were made for toolcall_gateway-1.0.0-py3-none-any.whl:

Publisher: ci.yml on caiwuu/toolcall-gateway

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