alibaba/open-code-review · error

invalid JSON for llm.extra_body: %w

Error message

invalid JSON for llm.extra_body: %w

What it means

Fires when the value for `ocr config set llm.extra_body` is not valid JSON that unmarshals into a map[string]any. extra_body is merged into outgoing LLM request bodies, so it must be a JSON object; the setter parses it eagerly so malformed JSON (or JSON arrays/scalars) is rejected at set time instead of corrupting later API requests.

Source

Thrown at cmd/opencodereview/config_cmd.go:592

		cfg.ensureTelemetry()
		cfg.Telemetry.Enabled = b
	case "telemetry.exporter", "telemetry.Exporter":
		cfg.ensureTelemetry()
		cfg.Telemetry.Exporter = value
	case "telemetry.otlp_endpoint", "telemetry.OTLPEndpoint":
		cfg.ensureTelemetry()
		cfg.Telemetry.OTLPEndpoint = value
	case "telemetry.content_logging", "telemetry.ContentLog":
		b, err := strconv.ParseBool(value)
		if err != nil {
			return fmt.Errorf("invalid boolean for telemetry.content_logging: %w", err)
		}
		cfg.ensureTelemetry()
		cfg.Telemetry.ContentLog = b
	case "llm.extra_body", "llm.ExtraBody":
		var m map[string]any
		if err := json.Unmarshal([]byte(value), &m); err != nil {
			return fmt.Errorf("invalid JSON for llm.extra_body: %w", err)
		}
		cfg.Llm.ExtraBody = m
	case "llm.retry_codes", "llm.RetryCodes":
		codes, warnings, err := llm.ParseRetryCodes(value)
		if err != nil {
			return err
		}
		for _, w := range warnings {
			fmt.Fprintf(os.Stderr, "[ocr] WARNING: %s\n", w)
		}
		cfg.Llm.RetryCodes = codes
	default:
		return fmt.Errorf("unknown config key: %s\nSupported keys: %s\nProvider fields: api_key, api_key_cmd, url, protocol, model, models, auth_header, extra_body, extra_headers, retry_codes, aws_region, aws_profile\nProtocol values: anthropic, anthropic-bedrock, openai, openai-responses\nMCP server fields: type, command, args, env, url, headers, tools, setup", key, strings.Join(supportedConfigKeys, ", "))
	}
	return nil
}

func applyProviderField(providerName string, entry *ProviderEntry, field, key, value string) error {

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Pass a valid JSON object: `ocr config set llm.extra_body '{"temperature":0.2}'`
  2. Single-quote the JSON in bash so inner double quotes survive
  3. Validate with `echo '<value>' | jq .` before setting
  4. If you need an array or scalar body, it is not supported — the value must be an object

Example fix

// before
ocr config set llm.extra_body temperature=0.2
// after
ocr config set llm.extra_body '{"temperature":0.2}'
Defensive patterns

Strategy: validation

Validate before calling

var m map[string]any
if err := json.Unmarshal([]byte(v), &m); err != nil {
    return fmt.Errorf("llm.extra_body must be a JSON object: %v", err)
}
_ = runConfigSet("llm.extra_body", v)

Try / catch

if err := runConfigSet("llm.extra_body", v); err != nil {
    if strings.Contains(err.Error(), "invalid JSON for llm.extra_body") {
        fmt.Fprintf(os.Stderr, "not a JSON object: %s — try: '{\"temperature\":0.2}'\n", v)
    }
}

Prevention

When it happens

Trigger: `ocr config set llm.extra_body '<value>'` where the value is not a JSON object — single quotes inside, missing quotes around keys, a JSON array, or unquoted text.

Common situations: Shell quoting mangling the JSON (double quotes stripped); passing a list instead of an object; pasting JSON5/YAML with comments or trailing commas; using single-quoted shell strings with embedded double quotes incorrectly.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/aa258da8c7330f6c. Report an issue: GitHub.