ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

ds4-server 400s valid OpenAI tool-role image messages as invalid JSON

Open aj-img opened this issue 3 months ago • 3 comments

Summary

ds4-server rejects a well-formed OpenAI Chat Completions request that includes an inline PNG/JPEG data URI on a role=tool (or role=function) message. The HTTP status is 400 with "invalid JSON request", even though the body is valid JSON.

This breaks OpenAI-compatible coding agents that attach tool-result screenshots as image_url content parts on the tool message, which is the normal Chat Completions shape. A role=user data URI of the same image is accepted.

Repro

POST /v1/chat/completions with messages:

  1. role=user text
  2. role=assistant with a tool_calls entry
  3. role=tool whose content is an array of {type:text} plus {type:image_url, image_url:{url:"data:image/png;base64,..."}} (or jpeg)

Observed: 400 {"error":{"message":"invalid JSON request","type":"invalid_request_error"}} before vision encode.

The same data URI on role=user returns 200.

Cause

In parse_messages / parse_anthropic_messages, any message with images whose role is not exactly "user" fails parse. That path always reports "invalid JSON request".

The user-only restriction matches the GLM note that image tokens should land on a user-shaped turn, and ds4-agent already remaps view_image onto a user turn. The server still needs to accept the OpenAI wire format: DeepSeek already wraps tool results in a user turn (<|User|><tool_result>…).

File paths and remote URLs should stay rejected.

Suggested fix

Allow inline images on user, tool, and function. Keep rejecting assistant/system images, paths, and https URLs. Prefer a specific error string for remaining schema failures instead of "invalid JSON request" (optional, not required for the role check).

I can send a PR with a parser unit test for the tool-role shape.

aj-img avatar Sep 01 '26 18:09 aj-img

Opened a PR implementing this: #944 — the role check now allows user/tool/function while keeping assistant/system/paths/https rejected, plus a parser unit test and an end-to-end HTTP verification with a real screenshot. Thanks for the precise diagnosis, made this quick to fix correctly.

Flor1an-B avatar Sep 02 '26 07:09 Flor1an-B

glad i could help. will keep sending as I see them (if i see them) On Sep 2, 2026 at 12:52 +0530, Flor1an-B (FBE) @.***>, wrote:

Flor1an-B left a comment (antirez/ds4#933) Opened a PR implementing this: #944 — the role check now allows user/tool/function while keeping assistant/system/paths/https rejected, plus a parser unit test and an end-to-end HTTP verification with a real screenshot. Thanks for the precise diagnosis, made this quick to fix correctly. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

aj-img avatar Sep 02 '26 14:09 aj-img

Confirming this on current main (0aaea5a, 2026-09-20) on an M4 Max 64 GB running Qwen3.8-Flash-Next-Q2.gguf with --vision.

Real-world client that sends this shape: Hermes Agent's vision_analyze tool. When the main model is marked vision-capable, Hermes returns the loaded image as an image_url part inside the tool-role message so the model can see it on the next turn. ds4-server answers 400 {"error":{"message":"invalid JSON request"}}, Hermes treats that as non-retryable, and the user sees a generic "model provider failed" message. Every later turn that reaches for the tool fails the same way.

Minimal reproduction (3 messages: user → assistant with tool_calls → tool result), same body except for the tool content:

tool content result
"image loaded" 200
[{"type":"text","text":"image loaded"}] 200
[{"type":"text",...},{"type":"image_url","image_url":{"url":"data:image/jpeg;base64,..."}}] 400 invalid JSON request

So the body parses fine; it is the role check at ds4_server.c:2133 (if (msg.images.len && strcmp(msg.role, "user")) goto fail;) that rejects it, and the message text is misleading.

Fix check: #944 still applies cleanly on top of 0aaea5a. With it, the image-in-tool-message request returns 200 with a correct description of the image, the model's response goes through Hermes normally, and the DS4_SERVER_TEST suite passes. I've been running that build as my daily server since today.

Small suggestion independent of #944: even where a role/image combination is rejected on purpose, returning a specific message (e.g. images are only accepted in user or tool messages) instead of invalid JSON request would let clients fall back sensibly. Hermes, for one, has a built-in "strip images from tool results and retry" path that keys on the error text and never fires on this one.

pswai avatar Sep 23 '26 11:09 pswai