Skip to content

Read the model from the body whatever the Content-Type - #238

Merged
thushan merged 8 commits into
mainfrom
fix/body-decides-model-extraction
Oct 27, 2026
Merged

thushan merged 8 commits into
mainfrom
fix/body-decides-model-extraction

Conversation

@thushan

@thushan thushan commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

body_inspector.go skipped body inspection whenever Content-Type wasn't JSON. That covered curl's default -d (form-urlencoded), text/plain, a missing header and vnd.api+json. A named request such as {"model":"nomic-embed-text"} then routed as if it named no model, to any compatible host, including hosts that don't serve the model.

The inspector now decides from the body:

  • Multipart bodies, any Content-Encoding, and bodies whose first non-whitespace byte isn't { are skipped.
  • It peeks through a 256-byte window. Leading whitespace is capped at 4 KiB, so scanning stays linear.
  • The body is restored byte-for-byte. On a read error, the consumed prefix is restored and the error is replayed.
  • Chunked bodies over 1 MiB behave as on main.

Type of change

  • Bug fix
  • Documentation

How was this tested?

New tests:

  • TestBodyInspector_BodyDecidesNotContentType: 24 content-type and body shapes.
  • TestBodyInspector_PeekWindowBoundary.
  • TestBodyInspector_ChunkedLimitBoundary.
  • TestBodyInspector_PartialBodySurvivesReadError.
  • TestBodyInspector_LeadingWhitespaceIsBounded: counts bytes read, not time.
  • TestProxyRouting_NamedModelIgnoresContentType.

The test that used to require skipping text/plain now expects the model.

The inspector benchmark went from 54 to 57 allocs/op with no measurable latency change.

Live run on integration/test-october at 85f9244, re-checked at c9d134c: a named BAAI/bge-m3 sent as form, text/plain, no header and JSON reached only the vLLM host every time.

Checklist

  • make ready passes at 67a8de7
  • Tests added or updated for the change
  • Documentation updated: api-reference/overview.md (Content-Type is optional), concepts/model-aliases.md, and the v0.0.30 release note
  • No new dependencies added

Out of scope: real form and multipart bodies still route as if no model was named.

Summary by CodeRabbit

  • New Features
    • Model routing can now identify a model in a JSON object body regardless of the Content-Type header, while forwarding the request body and header unchanged.
    • Multipart, form-encoded and compressed request bodies are not inspected.
  • Documentation
    • Clarified that Content-Type: application/json is recommended, not required, and that the request body must be a JSON object with a top-level model field.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Walkthrough

Body inspection now identifies eligible JSON-object request bodies without relying on Content-Type. The body is restored for downstream readers. Tests, API guidance, troubleshooting guidance, and release notes cover the updated behaviour.

Changes

Request body inspection and routing

Layer / File(s) Summary
Body inspection and restoration
internal/core/constants/content.go, internal/adapter/inspector/body_inspector.go, internal/adapter/inspector/body_inspector_test.go
Inspection now skips multipart and content-encoded bodies, and checks other bodies for a JSON-object prefix. Tests cover model extraction, bounded scanning, body restoration, and read and close behaviour.
Routing verification and documentation
internal/app/handlers/handler_proxy_body_content_type_test.go, docs/content/api-reference/overview.md, docs/content/concepts/model-aliases.md, docs/content/about/release-notes.md
The proxy-routing test checks named-model selection across Content-Type variants and confirms unchanged body forwarding. The documentation describes the updated body-inspection behaviour.

Priority: Not assessed

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related PRs

  • thushan/olla#235: Both changes affect model-based request routing. PR #235 changes how bare Ollama model names resolve for routing, statistics, and sticky keys.

Suggested reviewers: dnnspaul

Merge Risk: Low · up to d4513

A narrow read-error case may be hidden from downstream readers, and troubleshooting advice can mislead users whose bodies are excluded from inspection. These should be fixed, but the current evidence indicates bounded merge risk.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thushan
thushan merged commit fa1564e into main Oct 27, 2026
7 of 8 checks passed
@thushan
thushan deleted the fix/body-decides-model-extraction branch October 27, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant