alibaba/open-code-review · error

invalid JSON for %s: %w

Error message

invalid JSON for %s: %w

What it means

This error is returned by applyProviderField when the value supplied for a provider's 'extra_body' field is not valid JSON. The tool attempts json.Unmarshal on the raw string and, on failure, wraps the underlying parse error so the developer sees both which field failed and why. It is a validation guard that keeps malformed JSON out of the stored ProviderEntry.ExtraBody.

Source

Thrown at cmd/opencodereview/config_cmd.go:656

		}
	case "model":
		entry.Model = value
	case "models":
		models, err := parseModelListValue(value)
		if err != nil {
			return fmt.Errorf("invalid model list for %s: %w", key, err)
		}
		entry.Models = models
	case "auth_header":
		normalized, err := llm.NormalizeAuthHeader(value)
		if err != nil {
			return err
		}
		entry.AuthHeader = normalized
	case "extra_body":
		var m map[string]any
		if err := json.Unmarshal([]byte(value), &m); err != nil {
			return fmt.Errorf("invalid JSON for %s: %w", key, err)
		}
		entry.ExtraBody = m
	case "extra_headers":
		parsed, err := llm.ParseExtraHeaders(value)
		if err != nil {
			return fmt.Errorf("invalid extra headers for %s: %w", key, err)
		}
		entry.ExtraHeaders = parsed
	case "retry_codes":
		codes, warnings, err := llm.ParseRetryCodes(value)
		if err != nil {
			return fmt.Errorf("invalid retry codes for %s: %w", key, err)
		}
		for _, w := range warnings {
			fmt.Fprintf(os.Stderr, "[ocr] WARNING: %s\n", w)
		}
		entry.RetryCodes = codes
	case "aws_region", "aws_profile":

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Validate the JSON with `echo '<value>' | jq .` (or python -m json.tool) before setting it
  2. Single-quote the value in the shell so inner double quotes survive: ocr config set custom_providers.myprovider.extra_body '{"temperature":0.5}'
  3. Re-run the config set command with corrected JSON

Example fix

// before
ocr config set custom_providers.foo.extra_body {"temperature":0.5,}
// after
ocr config set custom_providers.foo.extra_body '{"temperature":0.5}'
Defensive patterns

Strategy: validation

Validate before calling

value='{"temperature":0.5}'
echo "$value" | jq -e . >/dev/null && ocr config set custom_providers.foo.extra_body "$value" || echo "invalid JSON"

Try / catch

if err := json.Unmarshal([]byte(value), &m); err != nil {
    return fmt.Errorf("invalid JSON for extra_body: %w", err)
}

Prevention

When it happens

Trigger: Running `ocr config set custom_providers.<name>.extra_body <value>` (or providers.<name>.extra_body) where <value> is not parseable JSON, e.g. unquoted keys, trailing commas, or a value with shell-mangled quotes like {"temperature":0.5 without closing braces.

Common situations: Pasting an extra-body JSON snippet into a shell without single-quoting it so the shell strips double quotes; hand-editing config and forgetting a comma; copy-pasting JSON5/YAML instead of strict JSON.

Understand the failure class

Related errors


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