Skip to content

[ML] Allowing null function name for chat completion schema #127976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private static class FunctionParser {

static {
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(ARGUMENTS_FIELD));
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(NAME_FIELD));
PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), new ParseField(NAME_FIELD));
}

public static StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice.Delta.ToolCall.Function parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.util.List;

import static org.hamcrest.Matchers.is;

public class OpenAiUnifiedStreamingProcessorTests extends ESTestCase {

public void testJsonLiteral() {
Expand Down Expand Up @@ -182,6 +184,73 @@ public void testJsonLiteralCornerCases() {
}
}

public void testJsonNullFunctionName() throws IOException {
String json = """
{
"object": "chat.completion.chunk",
"id": "",
"created": 1746800254,
"model": "/repository",
"system_fingerprint": "3.2.3-sha-a1f3ebe",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"tool_calls": [
{
"index": 0,
"id": "8f7c27be-6803-48e6-bba4-8cdcbcd2ff9a",
"type": "function",
"function": {
"name": null,
"arguments": " \\\""
}
}
]
},
"logprobs": null,
"finish_reason": null
}
],
"usage": null
}
""";

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, json)) {
StreamingUnifiedChatCompletionResults.ChatCompletionChunk chunk = OpenAiUnifiedStreamingProcessor.ChatCompletionChunkParser
.parse(parser);

// Assertions to verify the parsed object
assertThat(chunk.id(), is(""));
assertThat(chunk.model(), is("/repository"));
assertThat(chunk.object(), is("chat.completion.chunk"));
assertNull(chunk.usage());

List<StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice> choices = chunk.choices();
assertThat(choices.size(), is(1));

// First choice assertions
StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice firstChoice = choices.get(0);
assertNull(firstChoice.delta().content());
assertNull(firstChoice.delta().refusal());
assertThat(firstChoice.delta().role(), is("assistant"));
assertNull(firstChoice.finishReason());
assertThat(firstChoice.index(), is(0));

List<StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice.Delta.ToolCall> toolCalls = firstChoice.delta()
.toolCalls();
assertThat(toolCalls.size(), is(1));

StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice.Delta.ToolCall toolCall = toolCalls.get(0);
assertThat(toolCall.index(), is(0));
assertThat(toolCall.id(), is("8f7c27be-6803-48e6-bba4-8cdcbcd2ff9a"));
assertThat(toolCall.type(), is("function"));
assertNull(toolCall.function().name());
assertThat(toolCall.function().arguments(), is(" \""));
}
}

public void testOpenAiUnifiedStreamingProcessorParsing() throws IOException {
// Generate random values for the JSON fields
int toolCallIndex = randomIntBetween(0, 10);
Expand Down