{"record":{"id":"004bc07aeb4c97cd","repo":"plandex-ai/plandex","slug":"error-marshaling-request-w","errorCode":null,"errorMessage":"error marshaling request: %w","messagePattern":"error marshaling request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/model/client.go","lineNumber":329,"sourceCode":"\t\tif extendedReq.ExtraHeaders == nil {\n\t\t\textendedReq.ExtraHeaders = make(map[string]string)\n\t\t}\n\t\textendedReq.ExtraHeaders[\"anthropic-beta\"] = shared.AnthropicClaudeMaxBetaHeader\n\t\textendedReq.ExtraHeaders[\"Authorization\"] = \"Bearer \" + authVars[shared.AnthropicClaudeMaxTokenEnvVar]\n\t\textendedReq.ExtraHeaders[\"anthropic-product\"] = \"claude-code\"\n\n\t}\n\n\t// Marshal the request body to JSON\n\tvar jsonBody []byte\n\tvar err error\n\tif openaiReq != nil {\n\t\tjsonBody, err = json.Marshal(openaiReq)\n\t} else {\n\t\tjsonBody, err = json.Marshal(extendedReq)\n\t}\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error marshaling request: %w\", err)\n\t}\n\n\t// log.Println(\"request jsonBody\", string(jsonBody))\n\n\t// Create new request\n\tbaseUrl := baseModelConfig.BaseUrl\n\turl := baseUrl + \"/chat/completions\"\n\n\treq, err := http.NewRequestWithContext(ctx, \"POST\", url, bytes.NewReader(jsonBody))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating request: %w\", err)\n\t}\n\n\t// Set required headers for streaming\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\"Accept\", \"text/event-stream\")\n\treq.Header.Set(\"Cache-Control\", \"no-cache\")\n\treq.Header.Set(\"Connection\", \"keep-alive\")","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/model/client.go#L311-L347","documentation":"Before sending the streaming request, the library serializes either the OpenAI-shaped request (openaiReq) or the extended request to JSON with json.Marshal. If marshaling fails, it returns \"error marshaling request: %w\". For requests built from these struct types this is rare and almost always indicates a value that Go's json package cannot encode (e.g. an unsupported type such as a channel/func in an extension field).","triggerScenarios":"createChatCompletionStreamExtended marshals openaiReq (provider == OpenAI, after extendedReq.ToOpenAI()) or the raw extendedReq, and json.Marshal returns an error — typically an UnsupportedTypeError from a custom extension field of unserializable type, or a Marshaler implementation returning an error.","commonSituations":"A custom field added to ExtendedChatCompletionRequest/ExtendedOpenAIChatCompletionRequest without json tags or with a non-serializable type (chan, func, complex); a custom MarshalJSON that errors; NaN/Inf float values in custom sampling params.","solutions":["Read the wrapped %w error to identify the exact field/type that failed to marshal.","Audit recently added fields on ExtendedChatCompletionRequest / ExtendedOpenAIChatCompletionRequest for json tags and serializable types.","Add a unit test that marshals a fully-populated request struct to catch regressions.","Implement json.Marshaler on custom types if they need special encoding."],"exampleFix":"// before\ntype ExtendedChatCompletionRequest struct {\n    Callback func() `json:\"callback\"` // unserializable\n}\n// after\ntype ExtendedChatCompletionRequest struct {\n    CallbackID string `json:\"callback_id,omitempty\"` // serializable reference\n}","handlingStrategy":"try-catch","validationCode":"// sanity-check the request serializes before calling the library\nif _, err := json.Marshal(req); err != nil {\n    return fmt.Errorf(\"request is not JSON-serializable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"stream, err := client.CreateChatCompletionStream(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"error marshaling request\") {\n    log.Printf(\"request struct not serializable: %v\", err)\n    return fmt.Errorf(\"internal: request contains an unsupported field type: %w\", err)\n}","preventionTips":["Add json tags to every field on request structs; run lint rules enforcing tags.","Keep unit tests that marshal fully-populated request structs.","Avoid chan/func/complex fields on request types; reference them by ID instead.","Guard custom MarshalJSON implementations to never return errors for valid states."],"tags":["go","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}