Skip to content

Commit a17b7cd

Browse files
committed
improve coverage for tool choice
1 parent 9e1afcf commit a17b7cd

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/ChatCompletionServiceTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,36 @@ class ChatCompletionServiceTest extends OpenAiTest {
194194
toolDefinitions[0].schema.required == ["name"]
195195
}
196196

197+
def "create chat/completion test with tool choice"() {
198+
runUnderTrace("parent") {
199+
openAiClient.chat().completions().create(chatCompletionCreateParamsWithToolChoice())
200+
}
201+
202+
expect:
203+
List<LLMObs.LLMMessage> outputTag = []
204+
List<Map<String, Object>> toolDefinitions = []
205+
assertChatCompletionTrace(false, outputTag, [tool_choice: "function"], true, toolDefinitions)
206+
and:
207+
toolDefinitions.size() == 1
208+
toolDefinitions[0].name == "extract_student_info"
209+
toolDefinitions[0].description == "Get the student information from the body of the input text"
210+
and:
211+
outputTag.size() == 1
212+
LLMObs.LLMMessage outputMsg = outputTag.get(0)
213+
outputMsg.toolCalls.size() == 1
214+
def toolcall = outputMsg.toolCalls.get(0)
215+
toolcall.name == "extract_student_info"
216+
toolcall.toolId instanceof String
217+
toolcall.type == "function"
218+
toolcall.arguments == [
219+
name: 'David Nguyen',
220+
major: 'computer science',
221+
school: 'Stanford University',
222+
grades: 3.8,
223+
clubs: ['Chess Club', 'South Asian Student Association']
224+
]
225+
}
226+
197227
def "create streaming chat/completion test with tool calls"() {
198228
runnableUnderTrace("parent") {
199229
StreamResponse<ChatCompletionChunk> streamCompletion = openAiClient.chat().completions().createStreaming(chatCompletionCreateParamsWithTools())

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/OpenAiTest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.openai.models.Reasoning
1414
import com.openai.models.ReasoningEffort
1515
import com.openai.models.chat.completions.ChatCompletionCreateParams
1616
import com.openai.models.chat.completions.ChatCompletionFunctionTool
17+
import com.openai.models.chat.completions.ChatCompletionNamedToolChoice
1718
import com.openai.models.chat.completions.ChatCompletionStreamOptions
1819
import com.openai.models.completions.CompletionCreateParams
1920
import com.openai.models.embeddings.EmbeddingCreateParams
@@ -262,6 +263,17 @@ He hopes to pursue a career in software engineering after graduating.""")
262263
.build()
263264
}
264265

266+
ChatCompletionCreateParams chatCompletionCreateParamsWithToolChoice() {
267+
chatCompletionCreateParamsWithTools().toBuilder()
268+
.toolChoice(ChatCompletionNamedToolChoice.builder()
269+
.type(JsonValue.from("function"))
270+
.function(ChatCompletionNamedToolChoice.Function.builder()
271+
.name("extract_student_info")
272+
.build())
273+
.build())
274+
.build()
275+
}
276+
265277
ChatCompletionCreateParams chatCompletionCreateParamsWithRawTools() {
266278
def functionMap = [
267279
name: "extract_student_info_raw",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
method: POST
2+
path: chat/completions
3+
-- begin request body --
4+
{"messages":[{"content":"David Nguyen is a sophomore majoring in computer science at Stanford University and has a GPA of 3.8.\nDavid is an active member of the university's Chess Club and the South Asian Student Association.\nHe hopes to pursue a career in software engineering after graduating.","role":"user"}],"model":"gpt-4o-mini","tool_choice":{"function":{"name":"extract_student_info"},"type":"function"},"tools":[{"function":{"name":"extract_student_info","description":"Get the student information from the body of the input text","parameters":{"type":"object","properties":{"name":{"type":"string","description":"Name of the person"},"major":{"type":"string","description":"Major subject."},"school":{"type":"string","description":"The university name."},"grades":{"type":"integer","description":"GPA of the student."},"clubs":{"type":"array","description":"School clubs for extracurricular activities. ","items":{"type":"string","description":"Name of School Club"}}}}},"type":"function"}]}
5+
-- end request body --
6+
status code: 200
7+
-- begin response headers --
8+
access-control-expose-headers: X-Request-ID
9+
alt-svc: h3=":443"; ma=86400
10+
cf-cache-status: DYNAMIC
11+
cf-ray: 9e52289bcf4b6811-SEA
12+
content-type: application/json
13+
date: Tue, 31 Mar 2026 20:38:37 GMT
14+
openai-organization: datadog-staging
15+
openai-processing-ms: 975
16+
openai-project: proj_gt6TQZPRbZfoY2J9AQlEJMpd
17+
openai-version: 2020-10-01
18+
server: cloudflare
19+
set-cookie: __cf_bm=8UpsxSUpYP5PmHi3NoNcvUAPHElwZCix7hG5h.Jprr8-1774989516.1296408-1.0.1.1-2GEVgpgzsaA8PIC.4pr9thdXAw7zglpkJZF_LD08k1itVMbyhtxUjskkov5EnFuRLGkMaXiKrl5sngZSQF3mFA_h_Cfh.bCgd.k3ovK4p9VcbEmkfWy7MStLsa5i3LFh; HttpOnly; Secure; Path=/; Domain=api.openai.com; Expires=Tue, 31 Mar 2026 21:08:37 GMT
20+
strict-transport-security: max-age=31536000; includeSubDomains; preload
21+
x-content-type-options: nosniff
22+
x-openai-proxy-wasm: v0.1
23+
x-ratelimit-limit-requests: 30000
24+
x-ratelimit-limit-tokens: 150000000
25+
x-ratelimit-remaining-requests: 29999
26+
x-ratelimit-remaining-tokens: 149999927
27+
x-ratelimit-reset-requests: 2ms
28+
x-ratelimit-reset-tokens: 0s
29+
x-request-id: req_6de1d82c867f4dc3a10eb169eda01c8f
30+
-- end response headers --
31+
-- begin response body --
32+
{
33+
"id": "chatcmpl-DPa96rNBMqoTGuLjKfIw3huHXDuai",
34+
"object": "chat.completion",
35+
"created": 1774989516,
36+
"model": "gpt-4o-mini-2024-07-18",
37+
"choices": [
38+
{
39+
"index": 0,
40+
"message": {
41+
"role": "assistant",
42+
"content": null,
43+
"tool_calls": [
44+
{
45+
"id": "call_FPVMvOQnA8cKnmYI83lMmQ75",
46+
"type": "function",
47+
"function": {
48+
"name": "extract_student_info",
49+
"arguments": "{\"name\":\"David Nguyen\",\"major\":\"computer science\",\"school\":\"Stanford University\",\"grades\":3.8,\"clubs\":[\"Chess Club\",\"South Asian Student Association\"]}"
50+
}
51+
}
52+
],
53+
"refusal": null,
54+
"annotations": []
55+
},
56+
"logprobs": null,
57+
"finish_reason": "stop"
58+
}
59+
],
60+
"usage": {
61+
"prompt_tokens": 162,
62+
"completion_tokens": 34,
63+
"total_tokens": 196,
64+
"prompt_tokens_details": {
65+
"cached_tokens": 0,
66+
"audio_tokens": 0
67+
},
68+
"completion_tokens_details": {
69+
"reasoning_tokens": 0,
70+
"audio_tokens": 0,
71+
"accepted_prediction_tokens": 0,
72+
"rejected_prediction_tokens": 0
73+
}
74+
},
75+
"service_tier": "default",
76+
"system_fingerprint": "fp_1bf41b89ce"
77+
}
78+
79+
-- end response body --

0 commit comments

Comments
 (0)