siyuan-note/siyuan · error

streamed function name does not match the terminal response

Error message

streamed function name does not match the terminal response

What it means

The library validates that the function name streamed via output_item.added matches the function_call item in the terminal response. A name mismatch means the streamed deltas would assemble a call to a different function than the provider's final answer — executing it could invoke the wrong tool.

Solutions

  1. Check for and update an OpenAI-compatible gateway that rewrites tool names
  2. Ensure tool definitions use names the provider streams verbatim (avoid characters subject to sanitization)
  3. Retry; if consistent with one provider version, pin or downgrade the API version
  4. Fail the turn rather than executing a mismatched function name
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure tool names are provider-safe before sending
for i := range tools {
    if !regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(tools[i].Function.Name) {
        return fmt.Errorf("tool name %q may be rewritten by providers", tools[i].Function.Name)
    }
}

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "name does not match") {
        return errProviderProtocol // do not invoke any tool this turn
    }
    return err
}

Prevention

When it happens

Trigger: stream.responseToolCalls[index].Function.Name is non-empty, the terminal item.Name is non-empty, and the names differ for the same output index.

Common situations: Provider emitting a placeholder name during streaming and a different final name (protocol drift); gateways rewriting function names (e.g. sanitizing characters); parallel tool calls whose items got interleaved across indices.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/fa1bf43a2858f411. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/openai_completion.go:582

		}
	}
	output, err := MarshalOpenAIResponseOutput(response.Output)
	if err != nil {
		return err
	}
	stream.responseOutput = output

	for index, raw := range response.Output {
		item, ok := responseOutputItem(raw)
		if !ok || item.Type != "function_call" {
			continue
		}
		current := stream.responseToolCalls[index]
		if current.ID != "" && item.CallID != "" && current.ID != item.CallID {
			return errors.New("streamed function call ID does not match the terminal response")
		}
		if current.Function.Name != "" && item.Name != "" && current.Function.Name != item.Name {
			return errors.New("streamed function name does not match the terminal response")
		}
		if current.Function.Arguments != item.Arguments &&
			!strings.HasPrefix(item.Arguments, current.Function.Arguments) {
			return errors.New("streamed function arguments do not match the terminal response")
		}
		delta := openai.ToolCall{Type: openai.ToolTypeFunction}
		deltaIndex := index
		delta.Index = &deltaIndex
		changed := false
		if current.ID == "" && item.CallID != "" {
			delta.ID = item.CallID
			changed = true
		}
		if current.Function.Name == "" && item.Name != "" {
			delta.Function.Name = item.Name
			changed = true
		}
		if current.Function.Arguments != item.Arguments && strings.HasPrefix(item.Arguments, current.Function.Arguments) {

View on GitHub (pinned to 9f775e8a12)