Skip to content

chat/completions: assistant messages with tool_calls require content → HTTP 422 breaks multi-turn tool use #89

Closed

Description

@inureyes

Summary

Message.content is a required field during request deserialization. However, OpenAI-compatible clients omit (or send null for) content on assistant messages that carry tool_calls. As a result, the Json<ChatCompletionRequest> extractor rejects the follow-up request of a multi-turn tool-calling conversation with HTTP 422 (Failed to deserialize the JSON body into the target type: messages[N]: missing field 'content') before the handler runs. This breaks any tool-calling loop at the second round.

Impact

  • First request (system + user + tools) succeeds; the model returns a tool_calls response.
  • The client appends the assistant tool_calls message plus a tool result message, then sends the follow-up request.
  • That follow-up is rejected with 422 because the assistant message has no content key.
  • Net effect: multi-turn / agentic tool use against mlxcel never completes even a single tool round.

Observed downstream in Backend.AI GO's scheduler and agent tool loops (a Gemma MLX model with tools enabled, served with --jinja). llama-server tolerates the missing content field, so the incompatibility is mlxcel-specific.

Root cause

src/server/types/request.rs (confirmed in v0.0.30; Message struct at ~L274, content at ~L276):

pub struct Message {
    pub role: Role,
    pub content: MessageContent,   // ← required: not Option, no #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCallInMessage>>,
}

MessageContent is an untagged enum (Text(String) | Parts(Vec<ContentPart>)), so a missing key — and also an explicit null — fails to deserialize. axum's Json extractor maps the serde error to HTTP 422.

Reproduction

Against a running mlxcel-server socket (Gemma MLX model, --jinja):

A) assistant tool_calls message WITHOUT content (OpenAI-style) → 422

curl --unix-socket "$SOCK" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' \
  http://localhost/v1/chat/completions -d '{
  "model":"...","max_tokens":16,"stream":false,
  "messages":[
    {"role":"user","content":"hi"},
    {"role":"assistant","tool_calls":[{"id":"call_1","type":"function","function":{"name":"get_system_info","arguments":"{}"}}]},
    {"role":"tool","tool_call_id":"call_1","content":"mem: 12GB free"}
  ]}'

Result:

Failed to deserialize the JSON body into the target type: messages[1]: missing field `content`
HTTP 422

B) same request WITH content: "" on the assistant message → 200 OK

Adding "content":"" to the assistant tool_calls message makes the request deserialize and the completion succeed normally.

Expected behavior

Per the OpenAI Chat Completions spec, content is optional/nullable for assistant messages that contain tool_calls (the tool calls are the payload; content may be null or absent). The server should accept a missing or null content and treat it as empty.

Suggested fix

Make content optional with a default in Message, e.g.:

#[serde(default)]
pub content: MessageContent,

with impl Default for MessageContent { fn default() -> Self { MessageContent::Text(String::new()) } } — or change to content: Option<MessageContent> and treat None as empty in MessageContent::text(), build_chat_messages, and build_raw_json_messages.

Two follow-ups to keep in mind:

  1. build_raw_json_messages should still emit a "content" field (empty string when absent) so Jinja chat templates that reference message.content keep rendering.
  2. Also accept an explicit JSON null (via Option<MessageContent> or a custom deserializer), since some clients send "content": null rather than omitting the key.

Environment

  • mlxcel v0.0.30 (same code shape confirmed in v0.0.21)
  • Confirmed via live reproduction against a running mlxcel-server Unix socket.

Activity

  1. added
    type:bug
    area:inference
    status:done
    on May 26, 2026
  2. self-assigned this
    on May 29, 2026
  3. added this to the 0.1 milestone on Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencestatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions