|
20 | 20 | import org.junit.jupiter.api.BeforeEach; |
21 | 21 | import org.junit.jupiter.api.Test; |
22 | 22 |
|
23 | | -/** |
24 | | - * Covers {@link InstrumentationSemConv#addServerSideChildSpans}: only vendor-executed (server-side) |
25 | | - * tool calls in an OpenAI Responses body become child spans nested under the LLM span. Client-side |
26 | | - * calls ({@code function_call}, {@code computer_call}) are excluded. |
27 | | - */ |
| 23 | +/** Covers provider response tagging and {@link InstrumentationSemConv#addServerSideChildSpans}. */ |
28 | 24 | class InstrumentationSemConvTest { |
29 | 25 |
|
30 | 26 | private static final AttributeKey<String> SPAN_ATTRIBUTES = |
@@ -81,6 +77,44 @@ private static JsonNode json(String s) { |
81 | 77 | return BraintrustJsonMapper.fromJson(s, JsonNode.class); |
82 | 78 | } |
83 | 79 |
|
| 80 | + private SpanData tagOpenAIResponse(String responseBody) { |
| 81 | + Span span = tracer.spanBuilder("llm").startSpan(); |
| 82 | + try { |
| 83 | + InstrumentationSemConv.tagLLMSpanResponse( |
| 84 | + tracer, span, InstrumentationSemConv.PROVIDER_NAME_OPENAI, responseBody); |
| 85 | + } finally { |
| 86 | + span.end(); |
| 87 | + } |
| 88 | + return byName(exporter.getFinishedSpanItems(), "llm"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void tagsOpenAIAudioTranscriptionOutput() { |
| 93 | + SpanData span = tagOpenAIResponse("{\"text\":\"Hello from the recording.\"}"); |
| 94 | + |
| 95 | + assertEquals( |
| 96 | + json("{\"text\":\"Hello from the recording.\"}"), |
| 97 | + json(span.getAttributes().get(OUTPUT_JSON))); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void tagsOpenAIVerboseAudioTranscriptionOutput() { |
| 102 | + String body = |
| 103 | + """ |
| 104 | + { |
| 105 | + "text": "Hello from the recording.", |
| 106 | + "language": "english", |
| 107 | + "duration": 1.25, |
| 108 | + "segments": [{"id": 0, "text": "Hello from the recording."}], |
| 109 | + "words": [{"word": "Hello", "start": 0.0, "end": 0.4}] |
| 110 | + } |
| 111 | + """; |
| 112 | + |
| 113 | + SpanData span = tagOpenAIResponse(body); |
| 114 | + |
| 115 | + assertEquals(json(body), json(span.getAttributes().get(OUTPUT_JSON))); |
| 116 | + } |
| 117 | + |
84 | 118 | @Test |
85 | 119 | void emitsWebSearchCallToolSpanParentedToLlm() { |
86 | 120 | String body = |
|
0 commit comments