@@ -1501,6 +1501,65 @@ void streamToolCalls() {
15011501 Value .of (KeyValue .of ("content" , Value .of (finalAnswer )))))));
15021502 }
15031503
1504+ @ Test
1505+ void streamConnectionError () {
1506+ OpenAIClient client =
1507+ wrap (
1508+ OpenAIOkHttpClient .builder ()
1509+ .baseUrl ("http://localhost:9999/v5" )
1510+ .apiKey ("testing" )
1511+ .maxRetries (0 )
1512+ .build ());
1513+
1514+ ChatCompletionCreateParams params =
1515+ ChatCompletionCreateParams .builder ()
1516+ .messages (Collections .singletonList (createUserMessage (TEST_CHAT_INPUT )))
1517+ .model (TEST_CHAT_MODEL )
1518+ .build ();
1519+
1520+ Throwable thrown = catchThrowable (() -> client .chat ().completions ().createStreaming (params ));
1521+ assertThat (thrown ).isInstanceOf (OpenAIIoException .class );
1522+
1523+ getTesting ()
1524+ .waitAndAssertTraces (
1525+ trace ->
1526+ trace .hasSpansSatisfyingExactly (
1527+ maybeWithTransportSpan (
1528+ span ->
1529+ span .hasException (thrown )
1530+ .hasAttributesSatisfyingExactly (
1531+ equalTo (GEN_AI_SYSTEM , OPENAI ),
1532+ equalTo (GEN_AI_OPERATION_NAME , CHAT ),
1533+ equalTo (GEN_AI_REQUEST_MODEL , TEST_CHAT_MODEL )))));
1534+
1535+ getTesting ()
1536+ .waitAndAssertMetrics (
1537+ INSTRUMENTATION_NAME ,
1538+ metric ->
1539+ metric
1540+ .hasName ("gen_ai.client.operation.duration" )
1541+ .hasHistogramSatisfying (
1542+ histogram ->
1543+ histogram .hasPointsSatisfying (
1544+ point ->
1545+ point
1546+ .hasSumGreaterThan (0.0 )
1547+ .hasAttributesSatisfyingExactly (
1548+ equalTo (GEN_AI_SYSTEM , "openai" ),
1549+ equalTo (GEN_AI_OPERATION_NAME , "chat" ),
1550+ equalTo (GEN_AI_REQUEST_MODEL , TEST_CHAT_MODEL )))));
1551+
1552+ SpanContext spanCtx = getTesting ().waitForTraces (1 ).get (0 ).get (0 ).getSpanContext ();
1553+
1554+ getTesting ()
1555+ .waitAndAssertLogRecords (
1556+ log ->
1557+ log .hasAttributesSatisfyingExactly (
1558+ equalTo (GEN_AI_SYSTEM , OPENAI ), equalTo (EVENT_NAME , "gen_ai.user.message" ))
1559+ .hasSpanContext (spanCtx )
1560+ .hasBody (Value .of (KeyValue .of ("content" , Value .of (TEST_CHAT_INPUT )))));
1561+ }
1562+
15041563 protected static ChatCompletionMessageParam createUserMessage (String content ) {
15051564 return ChatCompletionMessageParam .ofUser (
15061565 ChatCompletionUserMessageParam .builder ()
0 commit comments