JuliusBrussee/caveman · error

openai compress: invalid splice range

Error message

openai compress: invalid splice range

What it means

During OpenAI response-body compression, spliceStringReplacements rewrites byte ranges of the JSON body with replacement bytes. Before splicing, it validates each candidate range is ordered, within the body bounds, and non-empty; if any range violates these invariants it aborts rather than emitting a corrupted body. This is an internal consistency check, so hitting it means the compression pass computed bad offsets.

Solutions

  1. Report the full request/response capture to the proxy maintainers with proxy version — this is an internal invariant failure, not user input error.
  2. Disable the response-body compression option (compress off) to bypass the faulty splice pass.
  3. Check whether the failing content type includes escaped Unicode or very large bodies and try a smaller/simpler payload to narrow the trigger.
  4. Pin to the previous proxy version where compression did not corrupt ranges.

Example fix

// before: transform computes offsets on unescaped text
start := strings.Index(unescaped, needle)
// after: compute offsets on the original raw body bytes
start := bytes.Index(body, []byte(needle))
Defensive patterns

Strategy: try-catch

Try / catch

resp, err := doCompressedRequest(req)
if err != nil {
    if strings.Contains(err.Error(), "invalid splice range") {
        // internal invariant failure: retry once with compression disabled
        return doPlainRequest(req)
    }
    return err
}

Prevention

When it happens

Trigger: The compression pipeline produces candidate ranges (c.start/c.end) that overlap a previous replacement (c.start < last), extend past len(body) (c.end > len(body)), or are empty/inverted (c.start >= c.end), when applying string-replacement edits to the response body.

Common situations: A new compression transform computes offsets against an escaped or re-encoded form of the body instead of the raw bytes; concurrent edits to the same region; a regex/JSON-path rewrite spanning replacement boundaries introduced in a newer proxy version.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/bb50a90e5d492109. Report an issue: GitHub.

Appendix: source

Thrown at proxy/providers/openai/content_compress.go:651

	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	enc.SetEscapeHTML(false)
	if err := enc.Encode(s); err != nil {
		return nil, err
	}
	return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil
}

func spliceStringReplacements(body []byte, candidates []spliceCandidate, reps [][]byte) ([]byte, error) {
	if len(reps) != len(candidates) {
		return nil, fmt.Errorf("openai compress: %d replacements for %d segments", len(reps), len(candidates))
	}
	var out []byte
	last := 0
	changed := false
	for i, c := range candidates {
		if c.start < last || c.end > len(body) || c.start >= c.end {
			return nil, fmt.Errorf("openai compress: invalid splice range")
		}
		rep := reps[i]
		if rep == nil || bytes.Equal(rep, c.original) {
			continue
		}
		quoted, err := quoteJSONStringNoHTML(string(rep))
		if err != nil {
			return nil, err
		}
		if !changed {
			out = make([]byte, 0, len(body)-len(c.original)+len(rep))
		}
		out = append(out, body[last:c.start]...)
		out = append(out, quoted...)
		last = c.end
		changed = true
	}
	if !changed {

View on GitHub (pinned to 3ee70a1026)