sigoden/aichat · error

Failed to parse '.tools

Error message

Failed to parse '.tools[{i}]'

What it means

parse_tools deserializes each entry of the request's 'tools' array, expecting {type:"function", function:{...}}. If the entry cannot be deserialized into a FunctionDeclaration, it bails with 'Failed to parse .tools[i]', where i is the array index of the offending tool.

Solutions

  1. Fix the tool at index i to match {type:'function', function:{name, description, parameters}}
  2. Ensure 'parameters' is a valid JSON Schema object (use {type:'object',properties:{}} if empty)
  3. Validate the request body against the OpenAI tools schema before sending
  4. Update the client SDK so it emits OpenAI-format tools

Example fix

// before
{"tools":[{"name":"get_weather","description":"..."}]}
// after
{"tools":[{"type":"function","function":{"name":"get_weather","description":"...","parameters":{"type":"object","properties":{}}}}]}
Defensive patterns

Strategy: validation

Validate before calling

function validateTools(tools) {
  tools.forEach((t, i) => {
    if (t?.type !== 'function' || typeof t.function?.name !== 'string' ||
        (t.function.parameters && typeof t.function.parameters !== 'object')) {
      throw new Error(`invalid tool at index ${i}`);
    }
  });
}

Type guard

const isFnTool = (t) => t != null && typeof t === 'object' && t.type === 'function' && typeof t.function === 'object' && t.function !== null;

Prevention

When it happens

Trigger: POST /v1/chat/completions whose tools[i].function is missing, not an object, or lacks required fields (name/description/parameters) per the function schema.

Common situations: Passing tools in a non-OpenAI format (e.g. Anthropic-style tool definitions); omitting the nested 'function' object; missing 'parameters' JSON schema; typos in field names.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of sigoden/aichat@82976d349a (2026-09-09). Data as JSON: /api/errors/bc7ee90b5e9e5c65. Report an issue: GitHub.

Appendix: source

Thrown at src/serve.rs:931

    Ok(output)
}

fn parse_tools(tools: Option<Vec<Value>>) -> Result<Option<Vec<FunctionDeclaration>>> {
    let tools = match tools {
        Some(v) => v,
        None => return Ok(None),
    };
    let mut functions = vec![];
    for (i, tool) in tools.into_iter().enumerate() {
        if let (Some("function"), Some(function)) = (
            tool["type"].as_str(),
            tool["function"]
                .as_object()
                .and_then(|v| serde_json::from_value(json!(v)).ok()),
        ) {
            functions.push(function);
        } else {
            bail!("Failed to parse '.tools[{i}]'")
        }
    }
    Ok(Some(functions))
}

View on GitHub (pinned to 82976d349a)