wavetermdev/waveterm · error

failed to marshal error output: %w

Error message

failed to marshal error output: %w

What it means

When a tool result carries an error (ErrorText != ""), the converter builds an OpenAIFunctionCallErrorOutput and JSON-marshals it; a marshal failure (practically impossible for this fixed struct, but defensively handled) aborts conversion wrapped with this message.

Source

Thrown at pkg/aiusechat/openai/openai-convertmessage.go:480

	var messages []*OpenAIChatMessage

	for _, result := range toolResults {
		if result.ToolUseID == "" {
			return nil, fmt.Errorf("tool result missing ToolUseID")
		}

		// Create the function call output with result data
		var outputData any
		if result.ErrorText != "" {
			// Marshal error output to string
			errorOutput := OpenAIFunctionCallErrorOutput{
				Ok:    "false",
				Error: result.ErrorText,
			}
			errorBytes, err := json.Marshal(errorOutput)
			if err != nil {
				return nil, fmt.Errorf("failed to marshal error output: %w", err)
			}
			outputData = string(errorBytes)
		} else {
			// Check if text looks like an image data URL
			if strings.HasPrefix(result.Text, "data:image/") {
				// Convert to output array with input_image type
				outputData = []OpenAIMessageContent{
					{
						Type:     "input_image",
						ImageUrl: result.Text,
					},
				}
			} else {
				// Use text result for success
				outputData = result.Text
			}
		}

View on GitHub (pinned to a4447c1563)

Solutions

  1. Verify ErrorText contains valid string data (it should be a plain string)
  2. If you patched OpenAIFunctionCallErrorOutput, ensure all fields are JSON-marshalable
  3. Update/restore the upstream library version if local modifications introduced the issue
Defensive patterns

Strategy: try-catch

Validate before calling

if result.ErrorText != "" {
    if _, err := json.Marshal(OpenAIFunctionCallErrorOutput{Ok: "false", Error: result.ErrorText}); err != nil {
        return fmt.Errorf("unmarshalable error output: %w", err)
    }
}

Try / catch

out, err := ConvertToolResultsToOpenAIChatMessage(results)
if err != nil && strings.Contains(err.Error(), "failed to marshal error output") {
    // fall back to a plain-text error output
}

Prevention

When it happens

Trigger: ConvertToolResultsToOpenAIChatMessage hits the ErrorText branch and json.Marshal of OpenAIFunctionCallErrorOutput returns an error — only possible if the struct gains fields that cannot be marshaled (e.g. channel/func values) in a modified build.

Common situations: Custom forks or locally patched versions of the struct with unmarshalable fields; otherwise this path is unreachable and indicates an unexpected internal failure.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/18f6229048ed7498. Report an issue: GitHub.