Skip to content

Sort request body keys across all HTTP providers - #441

Merged
mattt merged 1 commit into
huggingface:mainfrom
Pybsama:sort-provider-request-bodies
Dec 1, 2026
Merged

mattt merged 1 commit into
huggingface:mainfrom
Pybsama:sort-provider-request-bodies

Conversation

@Pybsama

@Pybsama Pybsama commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #432 and #437. OpenAILanguageModel and OllamaLanguageModel already sort JSON object keys in outgoing request bodies to keep prompt prefixes stable for exact-prefix prompt caching, while AnthropicLanguageModel, GeminiLanguageModel, and OpenResponsesLanguageModel still encoded [String: JSONValue] request parameters with a plain JSONEncoder(), producing non-deterministic key order across requests.

Changes

  • Extract a shared encodeRequestBody(_:) helper in Sources/AnyLanguageModel/Shared/HTTPTransport+Fetch.swift using JSONEncoder with .sortedKeys.
  • Use encodeRequestBody(_:) in AnthropicLanguageModel, GeminiLanguageModel, and OpenResponsesLanguageModel (both respond and streamResponse), and reuse it in OpenAILanguageModel and OllamaLanguageModel instead of duplicating per-model helpers.
  • Add ProviderRequestBodySortingTests covering streaming and non-streaming recorded request bodies via FakeTransport for Anthropic, Gemini, Open Responses, and Ollama.

Verification

  • swift format lint --strict --recursive . (Swift 6.3.3): passed.
  • swift test --disable-index-store --filter ProviderRequestBodySortingTests: reproduced 6 failures before the change (Anthropic, Gemini, Open Responses streaming & non-streaming) and passed all 4 tests (8 parameterized cases) after the change.
  • swift test --skip-build --disable-index-store: 1,114 / 1,114 non-live tests passed across 114 suites (only OllamaLanguageModelTests failed because no local Ollama server is running).

@mattt
mattt merged commit ab3a9ba into huggingface:main Dec 1, 2026
14 checks passed
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.

2 participants