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 modelstext2tool: 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: writesauto / required / none / specific functionconstraints into the tagged prompttext2tool: can validate the model output against the same constraints while parsing
Production Notes
- The public API raises stable exceptions from
toolcall_gateway.errors:ToolcallGatewayErrorTaggedOutputErrorToolChoiceError
- The package ships a
py.typedmarker 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
384f27c959d39b6dde70788602d7d0bf27fd71283bd41027b637847d0696c5b7
|
|
| MD5 |
f471e8f39dc78519685dbb44f22148b9
|
|
| BLAKE2b-256 |
7337789955a5479808ce0aad0e0c2e52b653a1dfcdf550d35cc66b6283c89666
|
Provenance
The following attestation bundles were made for toolcall_gateway-1.0.0.tar.gz:
Publisher:
ci.yml on caiwuu/toolcall-gateway
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
toolcall_gateway-1.0.0.tar.gz -
Subject digest:
384f27c959d39b6dde70788602d7d0bf27fd71283bd41027b637847d0696c5b7 - Sigstore transparency entry: 1227700168
- Sigstore integration time:
-
Permalink:
caiwuu/toolcall-gateway@8ac09fefe8345941bacc2210fe9940ed3b0ef4ae -
Branch / Tag:
refs/heads/master - Owner: https://github.com/caiwuu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@8ac09fefe8345941bacc2210fe9940ed3b0ef4ae -
Trigger Event:
push
-
Statement type:
File details
Details for the file toolcall_gateway-1.0.0-py3-none-any.whl.
File metadata
- Download URL: toolcall_gateway-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.0 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 |
82b5fdc3bd2b60518b81cedd7c2d5f88787fa64fb44be06f7c24698c1064ca8b
|
|
| MD5 |
0e043affd8d01da3b9d75fa42c917097
|
|
| BLAKE2b-256 |
3258a0072dddb2acb7a149d5b64f964e6bae09b0a155c44802a9eb06c77a6210
|
Provenance
The following attestation bundles were made for toolcall_gateway-1.0.0-py3-none-any.whl:
Publisher:
ci.yml on caiwuu/toolcall-gateway
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
toolcall_gateway-1.0.0-py3-none-any.whl -
Subject digest:
82b5fdc3bd2b60518b81cedd7c2d5f88787fa64fb44be06f7c24698c1064ca8b - Sigstore transparency entry: 1227700195
- Sigstore integration time:
-
Permalink:
caiwuu/toolcall-gateway@8ac09fefe8345941bacc2210fe9940ed3b0ef4ae -
Branch / Tag:
refs/heads/master - Owner: https://github.com/caiwuu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@8ac09fefe8345941bacc2210fe9940ed3b0ef4ae -
Trigger Event:
push
-
Statement type: