[PR #13332] Add strict JSON validation to reject unknown fields #14167

Open
opened 2026-04-13 00:47:10 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/13332
Author: @shoiam
Created: 12/4/2025
Status: 🔄 Open

Base: mainHead: strict-json-validation


📝 Commits (1)

  • e11b35a Add strict JSON validation to reject unknown fields. Fixes Issue ##7653

📊 Changes

5 files changed (+176 additions, -14 deletions)

View changed files

📝 api/types.go (+2 -3)
📝 middleware/openai.go (+33 -3)
server/bind.go (+23 -0)
📝 server/routes.go (+44 -8)
server/strict_validation_test.go (+74 -0)

📄 Description

Fixes #7653

Summary

Implemented strict JSON validation across all API endpoints to catch developer errors early. The API now rejects requests with unknown or misspelled fields instead of silently ignoring them.

Changes

  • Added strictBindJson function that uses DisallowUnknownFields()
  • Applied to all native API endpoints (/api/generate, /api/chat, /api/embed, /api/embeddings, /api/pull, /api/push, /api/delete, /api/copy)
  • Applied to OpenAI-compatible endpoints (/v1/chat/completions, /v1/completions, /v1/embeddings)
  • Provides clear error messages for unknown fields with helpful context
  • Added comprehensive unit tests in server/strict_validation_test.go

Error Message Examples

Before: Request silently ignores unknown field
After: {"error": "unknown field \"invalid_param\" in request"}

Test Results

=== RUN   TestStrictJSONValidation
=== RUN   TestStrictJSONValidation/GenerateHandler_Unknown_Field
=== RUN   TestStrictJSONValidation/ChatHandler_Unknown_Field
=== RUN   TestStrictJSONValidation/EmbedHandler_Unknown_Field
--- PASS: TestStrictJSONValidation (0.00s)
    --- PASS: TestStrictJSONValidation/GenerateHandler_Unknown_Field (0.00s)
    --- PASS: TestStrictJSONValidation/ChatHandler_Unknown_Field (0.00s)
    --- PASS: TestStrictJSONValidation/EmbedHandler_Unknown_Field (0.00s)
PASS

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/13332 **Author:** [@shoiam](https://github.com/shoiam) **Created:** 12/4/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `strict-json-validation` --- ### 📝 Commits (1) - [`e11b35a`](https://github.com/ollama/ollama/commit/e11b35a565840e1c5147ce253458283e697e5476) Add strict JSON validation to reject unknown fields. Fixes Issue ##7653 ### 📊 Changes **5 files changed** (+176 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `api/types.go` (+2 -3) 📝 `middleware/openai.go` (+33 -3) ➕ `server/bind.go` (+23 -0) 📝 `server/routes.go` (+44 -8) ➕ `server/strict_validation_test.go` (+74 -0) </details> ### 📄 Description Fixes #7653 ## Summary Implemented strict JSON validation across all API endpoints to catch developer errors early. The API now rejects requests with unknown or misspelled fields instead of silently ignoring them. ## Changes - Added `strictBindJson` function that uses `DisallowUnknownFields()` - Applied to all native API endpoints (`/api/generate`, `/api/chat`, `/api/embed`, `/api/embeddings`, `/api/pull`, `/api/push`, `/api/delete`, `/api/copy`) - Applied to OpenAI-compatible endpoints (`/v1/chat/completions`, `/v1/completions`, `/v1/embeddings`) - Provides clear error messages for unknown fields with helpful context - Added comprehensive unit tests in `server/strict_validation_test.go` ## Error Message Examples **Before:** Request silently ignores unknown field **After:** `{"error": "unknown field \"invalid_param\" in request"}` ## Test Results ``` === RUN TestStrictJSONValidation === RUN TestStrictJSONValidation/GenerateHandler_Unknown_Field === RUN TestStrictJSONValidation/ChatHandler_Unknown_Field === RUN TestStrictJSONValidation/EmbedHandler_Unknown_Field --- PASS: TestStrictJSONValidation (0.00s) --- PASS: TestStrictJSONValidation/GenerateHandler_Unknown_Field (0.00s) --- PASS: TestStrictJSONValidation/ChatHandler_Unknown_Field (0.00s) --- PASS: TestStrictJSONValidation/EmbedHandler_Unknown_Field (0.00s) PASS ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 00:47:10 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#14167