Repository navigation
fix: return 400 for wrong-typed provider fields instead of 500 - #439
Merged
Merged
Conversation
commit: |
The new /api/embeddings + /api/embed shape guard rejected two shapes that
answered 200 on main:
- `input` as number[] / number[][] (pre-tokenized). helpers.ts already
exports normalizeEmbeddingInput() for exactly this, and /v1/embeddings
accepts both shapes — route `input` through it instead of keeping a
parallel string-only rule.
- `prompt: null`, which the `embReq.prompt ?? embReq.input` fall-through
two lines below is designed to treat as absent. It also downgraded
{prompt:null,input:null} from the accurate "prompt or input field is
required" to "prompt field must be a string".
Genuinely wrong-typed values (prompt: 123, prompt: ["a"], input: 123,
mixed input: ["a", 1]) still 400, and mixed arrays now match the policy
/v1/embeddings already enforced.
…ds it
validateChatMessages is shared by Cohere, Ollama and Bedrock, but
`tool_calls` is not a field of the Anthropic Messages body at all — on
Bedrock tool calls are `tool_use` content blocks, and
bedrockToCompletionRequest ignores `msg.tool_calls` entirely.
Applying the rule there turned requests that used to match a fixture and
return 200 into 400s. Against a real server with a userMessage fixture
loaded, all three shapes regressed on both the invoke and the
invoke-with-response-stream paths:
tool_calls: [{id:"1"}] main 200 -> 400 "...must have a function object"
tool_calls: "x" main 200 -> 400 "...must be an array"
tool_calls: [null] main 200 -> 400 "...entries must be objects"
The rule stays on for Cohere and Ollama, whose converters do dereference
tool_calls[].function.name — both were 500s on main and are 400s now.
The six added `b !== null && b !== undefined` / `part !== null` clauses read as decorative over HTTP, because validateChatMessages rejects `content: [null]` before any converter runs. They are not: both bedrockToCompletionRequest and cohereToCompletionRequest are public exports, and reverting each clause individually makes its own case throw "Cannot read properties of null (reading 'type')". Cover all six through the exported converters (no logger, so the separate pre-existing unsupportedBlocks throw stays out of scope) so each clause now fails a test when removed rather than being unprovable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Wrong-typed fields pass the shallow presence checks and then throw inside the provider converters, surfacing as generic 500s instead of 400s. Verified crashes:
messages: [{content: 123}](cohere/ollama/bedrock),messages: [null],tools: "hi"(passes thelengthgate, throws on.map),tool_calls: "x",texts: [123]on/v2/embed(throws increateHash().update()),prompt: 123on Ollama/api/embeddings(throws on.slice()), stringcontents/ missingpartson Gemini, numericsystemon Bedrock.Changes
validateChatMessages()+validateToolsField()insrc/helpers.ts; a Gemini-specificvalidateGeminiBody()next to its converter. Missing/non-arraymessageskeeps the exact historic"Invalid request: messages array is required"message, and legacy check precedence (model before messages) is preserved — existing tests pass unchanged.messages[0].content must be a string or an array,tools must be an array, …) in cohere (chat + embed), ollama (chat + generate + embeddings), gemini, bedrock (invoke + stream), plus non-object body guards on all six.nullcontent coerces to""(OpenAI assistant tool-call messages), missing Geminipartsbehaves as no text, null entries in block filters are skipped instead of throwing.Tests
src/__tests__/wrong-type-validation.test.ts: 22 tests (helper unit tests + per-endpoint 400 assertions + non-500 tolerance cases for null content / part-less Gemini contents).cohere,cohere-embed,ollama,gemini,bedrock,bedrock-streamsuites (418 tests) still green;tsc,eslint,prettierclean.