{"record":{"id":"fa1bf43a2858f411","repo":"siyuan-note/siyuan","slug":"streamed-function-name-does-not-match-the-terminal","errorCode":null,"errorMessage":"streamed function name does not match the terminal response","messagePattern":"streamed function name does not match the terminal response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":582,"sourceCode":"\t\t}\n\t}\n\toutput, err := MarshalOpenAIResponseOutput(response.Output)\n\tif err != nil {\n\t\treturn err\n\t}\n\tstream.responseOutput = output\n\n\tfor index, raw := range response.Output {\n\t\titem, ok := responseOutputItem(raw)\n\t\tif !ok || item.Type != \"function_call\" {\n\t\t\tcontinue\n\t\t}\n\t\tcurrent := stream.responseToolCalls[index]\n\t\tif current.ID != \"\" && item.CallID != \"\" && current.ID != item.CallID {\n\t\t\treturn errors.New(\"streamed function call ID does not match the terminal response\")\n\t\t}\n\t\tif current.Function.Name != \"\" && item.Name != \"\" && current.Function.Name != item.Name {\n\t\t\treturn errors.New(\"streamed function name does not match the terminal response\")\n\t\t}\n\t\tif current.Function.Arguments != item.Arguments &&\n\t\t\t!strings.HasPrefix(item.Arguments, current.Function.Arguments) {\n\t\t\treturn errors.New(\"streamed function arguments do not match the terminal response\")\n\t\t}\n\t\tdelta := openai.ToolCall{Type: openai.ToolTypeFunction}\n\t\tdeltaIndex := index\n\t\tdelta.Index = &deltaIndex\n\t\tchanged := false\n\t\tif current.ID == \"\" && item.CallID != \"\" {\n\t\t\tdelta.ID = item.CallID\n\t\t\tchanged = true\n\t\t}\n\t\tif current.Function.Name == \"\" && item.Name != \"\" {\n\t\t\tdelta.Function.Name = item.Name\n\t\t\tchanged = true\n\t\t}\n\t\tif current.Function.Arguments != item.Arguments && strings.HasPrefix(item.Arguments, current.Function.Arguments) {","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L564-L600","documentation":"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.","triggerScenarios":"stream.responseToolCalls[index].Function.Name is non-empty, the terminal item.Name is non-empty, and the names differ for the same output index.","commonSituations":"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.","solutions":["Check for and update an OpenAI-compatible gateway that rewrites tool names","Ensure tool definitions use names the provider streams verbatim (avoid characters subject to sanitization)","Retry; if consistent with one provider version, pin or downgrade the API version","Fail the turn rather than executing a mismatched function name"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure tool names are provider-safe before sending\nfor i := range tools {\n    if !regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(tools[i].Function.Name) {\n        return fmt.Errorf(\"tool name %q may be rewritten by providers\", tools[i].Function.Name)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"name does not match\") {\n        return errProviderProtocol // do not invoke any tool this turn\n    }\n    return err\n}","preventionTips":["Use simple alphanumeric/underscore tool names","Avoid gateways that sanitize or alias function names","Verify parallel tool-call behavior of your provider"],"tags":["openai","tool-calls","name-mismatch","streaming"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}