The System One EventUtahGo To Event Page →

Docs

Set up Midium, use every part of it, run it for your team, and build on it.

Tool calling

OpenAI-style function calling on local models, kept reliable by constrained decoding plus a tolerant parser — and a built-in web_search tool.

For developers

Send function definitions in tools and the model can answer with tool_calls instead of text — the OpenAI format, on Chat completions and Anthropic Messages. Local models, especially small ones, tend to drift from their tool-call format as a conversation grows. Midium guards against that in two layers: a grammar that constrains the model's output while it writes a call, and a parser that recovers calls written in other common formats.

Request#

curl http://127.0.0.1:9100/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_MODEL",
    "messages": [{"role": "user", "content": "What is the weather in Denver?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Current weather for a city",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }
    }],
    "tool_choice": "auto"
  }'

Parameters#

NameTypeDescription
toolsarrayFunction tools: {"type": "function", "function": {"name", "description", "parameters"}}, with parameters a JSON Schema. Midium: {"type": "web_search"} adds Midium's own web search (below). Other tool types are dropped.
tool_choicestring or objectauto (default): the model decides. none: no tool calls. required: the model must call a tool. {"type": "function", "function": {"name": "get_weather"}}: it must call that tool. Midium: a tool's name as a plain string forces it too.

A forced tool must be in tools, or the request fails with 400 invalid_tool_choice.

Response#

JSON
{
  "id": "chatcmpl-…",
  "object": "chat.completion",
  "model": "YOUR_MODEL",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
          {
            "id": "call_8f2a61c0d9e44b7aa3c5f0e1",
            "type": "function",
            "function": {"name": "get_weather", "arguments": "{\"city\": \"Denver\"}"}
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {"prompt_tokens": 182, "completion_tokens": 21, "total_tokens": 203}
}
  • arguments is always a JSON string. Midium normalizes what the model wrote; arguments it can't turn into valid JSON fail the request with 400 invalid_tool_call.
  • finish_reason is tool_calls whenever the reply has tool calls.
  • Send each result back as a tool message with the call's tool_call_id, after the assistant message that made the call.

Streaming#

With "stream": true, tool calls arrive in delta.tool_calls: the call's id and function name first, then its arguments. The tool-call markup the model writes never appears in delta.content. The last chunk has finish_reason: "tool_calls".

{"type": "web_search"} in tools lets the model search the web. Midium runs the search itself (with Brave Search), adds the results to the conversation and lets the model answer — the search never comes back to you as a tool call. Up to three rounds of searching per request.

JSON
"tools": [{"type": "web_search"}]

It needs a Brave Search API key: add one in Settings → Advanced, or with midium setup --brave-key <key>. Without a key the shorthand is dropped and the model answers without searching. If you send your own function tool named web_search, that one is yours: it comes back as a normal tool call and the shorthand is ignored.

Notes#

  • Combining with response_format. When you send tools, the tool-call grammar takes the place of response_format. To get JSON and use tools, ask for the JSON in the final turn without tools, or define the output as a tool.
  • Forced tool, no call. If you force a tool and the model answers without calling it, the reply carries a call to that tool with empty arguments ({}).
  • Native formats. Models with their own tool-call format (Qwen, Gemma and others) use it, and Midium parses it into tool_calls. You always get the OpenAI shape.
  • Vision and tools. Vision models call tools too. Transcription models don't (400 unsupported_tools).
  • Anthropic Messages. On Anthropic Messages, tools use input_schema and calls come back as tool_use blocks.

Errors#

StatuscodeWhen
400invalid_tool_choicetool_choice names a tool that isn't in tools.
400invalid_tool_callThe model's arguments couldn't be turned into valid JSON. Retrying usually works.
400unsupported_toolsTools sent to a transcription model.

See Errors & limits.

Something here wrong or out of date? Tell us.