Skip to content

fix: return 400 for wrong-typed provider fields instead of 500 - #439

Merged
jpr5 merged 4 commits into
CopilotKit:mainfrom
Ayush7614:fix/wrong-type-field-400
Sep 13, 2026
Merged

jpr5 merged 4 commits into
CopilotKit:mainfrom
Ayush7614:fix/wrong-type-field-400

Conversation

@Ayush7614

Copy link
Copy Markdown
Contributor

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 the length gate, throws on .map), tool_calls: "x", texts: [123] on /v2/embed (throws in createHash().update()), prompt: 123 on Ollama /api/embeddings (throws on .slice()), string contents / missing parts on Gemini, numeric system on Bedrock.

Changes

  • New shared validateChatMessages() + validateToolsField() in src/helpers.ts; a Gemini-specific validateGeminiBody() next to its converter. Missing/non-array messages keeps the exact historic "Invalid request: messages array is required" message, and legacy check precedence (model before messages) is preserved — existing tests pass unchanged.
  • Handler-level 400s with precise messages (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.
  • Converter null-tolerance for legitimate traffic: null content coerces to "" (OpenAI assistant tool-call messages), missing Gemini parts behaves as no text, null entries in block filters are skipped instead of throwing.

Tests

  • New 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).
  • Verified: new suite green, plus cohere, cohere-embed, ollama, gemini, bedrock, bedrock-stream suites (418 tests) still green; tsc, eslint, prettier clean.

@pkg-pr-new

pkg-pr-new Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@439

commit: 1bb9622

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.
@jpr5
jpr5 merged commit a81ae46 into CopilotKit:main Sep 13, 2026
29 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