{"record":{"id":"fa18b162be76aa02","repo":"siyuan-note/siyuan","slug":"response-failed","errorCode":null,"errorMessage":"response failed","messagePattern":"response failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":707,"sourceCode":"\t\titem, ok := responseOutputItem(raw)\n\t\tif ok && item.Type == \"function_call\" {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc responseEventError(event openai.ResponseStreamEvent, fallback string) error {\n\tif event.Error != nil && event.Error.Message != \"\" {\n\t\treturn &openai.APIError{Code: event.Error.Code, Message: event.Error.Message}\n\t}\n\tif event.Response != nil && event.Response.Error != nil && event.Response.Error.Message != \"\" {\n\t\treturn &openai.APIError{Code: event.Response.Error.Code, Message: event.Response.Error.Message}\n\t}\n\tif event.Message != \"\" {\n\t\treturn &openai.APIError{Code: event.Code, Message: event.Message}\n\t}\n\treturn errors.New(fallback)\n}\n\nfunc (stream *OpenAICompletionStream) ResponseOutput() []json.RawMessage {\n\treturn CloneOpenAIResponseOutput(stream.responseOutput)\n}\n\nfunc (stream *OpenAICompletionStream) Close() {\n\tif stream.chat != nil {\n\t\tstream.chat.Close()\n\t}\n\tif stream.responses != nil {\n\t\tstream.responses.Close()\n\t}\n}\n","sourceCodeStart":689,"sourceCodeEnd":722,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L689-L722","documentation":"responseEventError builds an error from an OpenAI streaming response event when the remote API reports a failure inside the event stream. It prefers the embedded Response.Error (code + message), falls back to the event's own Code/Message, and finally to a generic fallback message. The 'response failed' string is that fallback used when neither the response object nor the event carries an error message.","triggerScenarios":"Streaming an OpenAI-compatible completion via OpenAICompletionStream.Recv when an event arrives with Response set but Response.Error empty, or with an empty event.Message and empty event.Code — i.e. the provider signals failure without populating error fields.","commonSituations":"Proxy or relay endpoints that close/abort the stream and emit a bare event; custom OpenAI-compatible servers that emit failure events without error payloads; truncated responses from misconfigured upstreams.","solutions":["Log the full raw event stream to identify what the provider actually returned","Verify the base URL/API endpoint is a valid OpenAI-compatible chat-completions endpoint","Check the API key and model name are valid for the configured provider","Retry the request; if intermittent, add stream retry/timeout handling in the caller"],"exampleFix":"// before\nstream, _ := util.OpenAICompletionStream(ctx, cfg, messages)\nfor {\n  ev, err := stream.Recv()\n  if err != nil { log.Fatalf(\"%v\", err) } // opaque \"response failed\"\n}\n// after\nstream, _ := util.OpenAICompletionStream(ctx, cfg, messages)\nfor {\n  ev, err := stream.Recv()\n  if err != nil {\n    var apiErr *openai.APIError\n    if errors.As(err, &apiErr) { log.Printf(\"api code=%s msg=%s\", apiErr.Code, apiErr.Message) }\n    log.Printf(\"stream failed (raw event: %+v): %v\", lastRawEvent, err)\n    break\n  }\n}","handlingStrategy":"try-catch","validationCode":"if stream == nil { return errors.New(\"stream not initialized\") }","typeGuard":null,"tryCatchPattern":"ev, err := stream.Recv()\nif err != nil {\n  var apiErr *openai.APIError\n  if errors.As(err, &apiErr) {\n    log.Printf(\"provider error code=%s: %s\", apiErr.Code, apiErr.Message)\n  } else {\n    log.Printf(\"stream failed: %v\", err)\n  }\n  return err\n}","preventionTips":["Test the provider endpoint with a minimal streaming request before wiring it in","Use well-known OpenAI-compatible endpoints rather than ad-hoc proxies","Add per-request timeouts and a bounded retry for transient stream failures","Log raw events at debug level to make empty-error failures diagnosable"],"tags":["openai","streaming","network"],"backgroundTag":"upstream-api-error","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"}