Skip to content

fix: remove empty Content block from tool-call-only messages#104

Open
zarusz wants to merge 1 commit intodevlooped:mainfrom
zarusz:feature/fix_empty_block
Open

fix: remove empty Content block from tool-call-only messages#104
zarusz wants to merge 1 commit intodevlooped:mainfrom
zarusz:feature/fix_empty_block

Conversation

@zarusz
Copy link

@zarusz zarusz commented Mar 25, 2026

Fix: Empty content block error on tool-call messages

Root cause

MapToRequest contained a workaround (added when the API was first
integrated) that padded any assistant message containing tool calls but
no text content with a synthetic empty Content block:

Status(StatusCode="InvalidArgument", Detail="Empty content block")

Grpc.Core.RpcException: Status(StatusCode="InvalidArgument", Detail="Empty content block") at xAI.GrokChatClient.GetResponseAsync(IEnumerable1 messages, ChatOptions options, CancellationToken cancellationToken) in /_/src/xAI/GrokChatClient.cs:line 40 at Microsoft.Extensions.AI.FunctionInvokingChatClient.GetResponseAsync(IEnumerable1 messages, ChatOptions options, CancellationToken cancellationToken) at Microsoft.Agents.AI.ChatClientAgent.RunCoreAsync(IEnumerable1 messages, AgentSession session, AgentRunOptions options, CancellationToken cancellationToken) at Microsoft.Agents.AI.ChatClientAgent.RunCoreAsync(IEnumerable1 messages, AgentSession session, AgentRunOptions options, CancellationToken cancellationToken) at 

The xAI gRPC API has since changed its validation: it now rejects
messages that contain an empty content block, returning:

This surfaces whenever the model performs a function/tool call, because
the subsequent round-trip includes an assistant message whose Contents
contains only FunctionCallContent items and no text.

Fix

Remove the padding block entirely. Assistant messages with only tool
calls are sent with an empty Content collection, which the API now
accepts without error.

Test

GrokDoesNotAddEmptyContentToToolCallOnlyMessages — a unit test (no
API key required) that drives a conversation containing a tool-call-only
assistant turn through MapToRequest via a mocked ChatClient and
asserts that no message in the outgoing request contains a zero-size
Content entry.

The xAI gRPC API previously required at least one Content entry per
message, including assistant messages that only carry tool calls (no
text). A workaround padded those messages with an empty `new Content()`
to satisfy that constraint.

The API has since reversed this requirement and now rejects any message
that contains an empty content block with:

  Status(StatusCode="InvalidArgument", Detail="Empty content block")

Remove the padding. Messages with tool calls and no text content are
now sent with an empty Content collection, which the API accepts.

Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
@CLAassistant
Copy link

CLAassistant commented Mar 25, 2026

CLA assistant check
All committers have signed the CLA.

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