|
7 | 7 | import dev.braintrust.TestHarness; |
8 | 8 | import dev.braintrust.instrumentation.Instrumenter; |
9 | 9 | import dev.langchain4j.agent.tool.Tool; |
| 10 | +import dev.langchain4j.agent.tool.ToolSpecification; |
10 | 11 | import dev.langchain4j.data.message.UserMessage; |
11 | 12 | import dev.langchain4j.model.chat.ChatModel; |
12 | 13 | import dev.langchain4j.model.chat.StreamingChatModel; |
| 14 | +import dev.langchain4j.model.chat.request.ChatRequest; |
| 15 | +import dev.langchain4j.model.chat.request.json.JsonObjectSchema; |
13 | 16 | import dev.langchain4j.model.chat.response.ChatResponse; |
14 | 17 | import dev.langchain4j.model.chat.response.StreamingChatResponseHandler; |
15 | 18 | import dev.langchain4j.model.openai.OpenAiChatModel; |
@@ -109,6 +112,9 @@ void testSyncChatCompletion() { |
109 | 112 | assertNotNull( |
110 | 113 | output.get(0).get("message").get("content"), |
111 | 114 | "Output should contain assistant response content"); |
| 115 | + |
| 116 | + // The serialized span output should reflect the full response the client received. |
| 117 | + assertSpanOutputReflects(response, span); |
112 | 118 | } |
113 | 119 |
|
114 | 120 | @Test |
@@ -239,6 +245,129 @@ public void onError(Throwable error) { |
239 | 245 | choice.get("message").get("content"), |
240 | 246 | "Output should contain the complete streamed response"); |
241 | 247 | assertNotNull(choice.get("finish_reason"), "Output should have finish_reason"); |
| 248 | + |
| 249 | + // The reconstructed streaming span output should reflect the full response the client |
| 250 | + // received — the instrumentation must feed every SSE event to the accumulator. |
| 251 | + assertSpanOutputReflects(response, llmSpan); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + @SneakyThrows |
| 256 | + void testStreamingChatCompletionWithTools() { |
| 257 | + // Auto-instrumentation intercepts OpenAiStreamingChatModel.Builder.build() |
| 258 | + StreamingChatModel model = |
| 259 | + OpenAiStreamingChatModel.builder() |
| 260 | + .apiKey(testHarness.openAiApiKey()) |
| 261 | + .baseUrl(testHarness.openAiBaseUrl()) |
| 262 | + .modelName("gpt-4o") |
| 263 | + .temperature(0.0) |
| 264 | + .build(); |
| 265 | + |
| 266 | + var weatherTool = |
| 267 | + ToolSpecification.builder() |
| 268 | + .name("get_weather") |
| 269 | + .description("Get the current weather for a location") |
| 270 | + .parameters( |
| 271 | + JsonObjectSchema.builder() |
| 272 | + .addStringProperty( |
| 273 | + "location", |
| 274 | + "The city and state, e.g. San" + " Francisco, CA") |
| 275 | + .required("location") |
| 276 | + .build()) |
| 277 | + .build(); |
| 278 | + |
| 279 | + var chatRequest = |
| 280 | + ChatRequest.builder() |
| 281 | + .messages(UserMessage.from("What is the weather in Paris, France?")) |
| 282 | + .toolSpecifications(weatherTool) |
| 283 | + .build(); |
| 284 | + |
| 285 | + var future = new CompletableFuture<ChatResponse>(); |
| 286 | + model.chat( |
| 287 | + chatRequest, |
| 288 | + new StreamingChatResponseHandler() { |
| 289 | + @Override |
| 290 | + public void onPartialResponse(String token) {} |
| 291 | + |
| 292 | + @Override |
| 293 | + public void onCompleteResponse(ChatResponse response) { |
| 294 | + future.complete(response); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public void onError(Throwable error) { |
| 299 | + future.completeExceptionally(error); |
| 300 | + } |
| 301 | + }); |
| 302 | + var response = future.get(); |
| 303 | + |
| 304 | + // The stream must carry tool-call deltas (merged by index) all the way to the span — the |
| 305 | + // original bug dropped tool_calls entirely from streaming reconstruction. |
| 306 | + assertTrue( |
| 307 | + response.aiMessage().hasToolExecutionRequests(), |
| 308 | + "Model should have requested a tool call"); |
| 309 | + |
| 310 | + var llmSpan = |
| 311 | + testHarness.awaitExportedSpans(1).stream() |
| 312 | + .filter(s -> s.getName().equals("Chat Completion")) |
| 313 | + .findFirst() |
| 314 | + .orElseThrow(() -> new AssertionError("no 'Chat Completion' llm span")); |
| 315 | + |
| 316 | + assertSpanOutputReflects(response, llmSpan); |
| 317 | + } |
| 318 | + |
| 319 | + /** |
| 320 | + * Asserts that the llm span's serialized output ({@code braintrust.output_json}) reflects the |
| 321 | + * full response the langchain client received — comparing the reconstructed assistant message |
| 322 | + * against the client's parsed {@link ChatResponse} (content, thinking, and tool calls) rather |
| 323 | + * than hand-asserting individual fields per test. langchain decodes the same stream |
| 324 | + * independently of our accumulator, so agreement is a meaningful end-to-end check. |
| 325 | + */ |
| 326 | + @SneakyThrows |
| 327 | + private void assertSpanOutputReflects(ChatResponse clientResponse, SpanData llmSpan) { |
| 328 | + String outputJson = |
| 329 | + llmSpan.getAttributes().get(AttributeKey.stringKey("braintrust.output_json")); |
| 330 | + assertNotNull(outputJson, "Span should have braintrust.output_json"); |
| 331 | + JsonNode message = JSON_MAPPER.readTree(outputJson).get(0).get("message"); |
| 332 | + assertNotNull(message, "Span output should contain a choice message"); |
| 333 | + |
| 334 | + var aiMessage = clientResponse.aiMessage(); |
| 335 | + |
| 336 | + if (aiMessage.text() != null) { |
| 337 | + assertEquals( |
| 338 | + aiMessage.text(), |
| 339 | + message.path("content").asText(), |
| 340 | + "Span output content should match the client's assistant text"); |
| 341 | + } |
| 342 | + if (aiMessage.thinking() != null) { |
| 343 | + assertEquals( |
| 344 | + aiMessage.thinking(), |
| 345 | + message.path("reasoning_content").asText(), |
| 346 | + "Span output reasoning_content should match the client's thinking"); |
| 347 | + } |
| 348 | + if (aiMessage.hasToolExecutionRequests()) { |
| 349 | + JsonNode toolCalls = message.get("tool_calls"); |
| 350 | + assertNotNull(toolCalls, "Span output should contain tool_calls"); |
| 351 | + var requests = aiMessage.toolExecutionRequests(); |
| 352 | + assertEquals( |
| 353 | + requests.size(), toolCalls.size(), "tool_calls count should match the client"); |
| 354 | + for (int i = 0; i < requests.size(); i++) { |
| 355 | + var request = requests.get(i); |
| 356 | + JsonNode function = toolCalls.get(i).get("function"); |
| 357 | + assertEquals( |
| 358 | + request.name(), function.get("name").asText(), "tool name should match"); |
| 359 | + assertEquals( |
| 360 | + JSON_MAPPER.readTree(request.arguments()), |
| 361 | + JSON_MAPPER.readTree(function.get("arguments").asText()), |
| 362 | + "tool arguments should match"); |
| 363 | + if (request.id() != null) { |
| 364 | + assertEquals( |
| 365 | + request.id(), |
| 366 | + toolCalls.get(i).get("id").asText(), |
| 367 | + "tool id should match"); |
| 368 | + } |
| 369 | + } |
| 370 | + } |
242 | 371 | } |
243 | 372 |
|
244 | 373 | @Test |
|
0 commit comments