Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/xAI.Tests/ChatClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,42 @@ public async Task GrokSetsToolCallIdOnlyWhenCallIdIsProvided()
Assert.False(toolMessage.HasToolCallId);
}

[Fact]
public async Task GrokDoesNotAddEmptyContentToToolCallOnlyMessages()
{
GetCompletionsRequest? capturedRequest = null;
var client = new Mock<xAI.Protocol.Chat.ChatClient>(MockBehavior.Strict);
client.Setup(x => x.GetCompletionAsync(It.IsAny<GetCompletionsRequest>(), null, null, CancellationToken.None))
.Callback<GetCompletionsRequest, Metadata?, DateTime?, CancellationToken>((req, _, _, _) => capturedRequest = req)
.Returns(CallHelpers.CreateAsyncUnaryCall(new GetChatCompletionResponse
{
Outputs =
{
new CompletionOutput
{
Message = new CompletionMessage { Content = "Done" }
}
}
}));

var grok = new GrokChatClient(client.Object, "grok-4-1-fast-non-reasoning");
var messages = new List<ChatMessage>
{
new(ChatRole.User, "What's the time?"),
// Assistant message with only a tool call and no text content
new(ChatRole.Assistant, [new FunctionCallContent("call-789", "get_time")]),
new(ChatRole.Tool, [new FunctionResultContent("call-789", "2024-01-01T00:00:00Z")]),
};

await grok.GetResponseAsync(messages);

Assert.NotNull(capturedRequest);
// Every Content item in every message must be non-empty; an empty Content block
// causes the API to return StatusCode="InvalidArgument", Detail="Empty content block".
Assert.DoesNotContain(capturedRequest.Messages,
m => m.Content.Any(c => c.CalculateSize() == 0));
}

[Fact]
public async Task GrokSendsDataContentAsBase64ImageUrl()
{
Expand Down
4 changes: 0 additions & 4 deletions src/xAI/GrokChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,6 @@ codeResult.RawRepresentation is ToolCall codeToolCall &&
if (gmsg.Content.Count == 0 && gmsg.ToolCalls.Count == 0)
continue;

// If we have only tool calls and no content, the gRPC enpoint fails, so add an empty one.
if (gmsg.Content.Count == 0)
gmsg.Content.Add(new Content());

request.Messages.Add(gmsg);
}

Expand Down