@@ -390,16 +390,192 @@ void clientToolUseAndPlainTextEmitNothing() {
390390 * braintrust.input_json} attribute (null when none was set).
391391 */
392392 private String inputJsonForRequest (String provider , String requestBody ) {
393+ return inputJsonForRequest (provider , List .of ("v1" , "messages" ), "POST" , requestBody );
394+ }
395+
396+ private String inputJsonForRequest (
397+ String provider , List <String > pathSegments , String method , String requestBody ) {
393398 Span span = tracer .spanBuilder ("llm" ).startSpan ();
394399 InstrumentationSemConv .tagLLMSpanRequest (
395- span ,
396- provider ,
397- "https://api.anthropic.com" ,
398- List .of ("v1" , "messages" ),
400+ span , provider , "https://api.example.com" , pathSegments , method , requestBody );
401+ span .end ();
402+ var spans = exporter .getFinishedSpanItems ();
403+ return spans .get (spans .size () - 1 ).getAttributes ().get (INPUT_JSON );
404+ }
405+
406+ private String responsesInput (String requestBody ) {
407+ return inputJsonForRequest (
408+ InstrumentationSemConv .PROVIDER_NAME_OPENAI ,
409+ List .of ("v1" , "responses" ),
399410 "POST" ,
400411 requestBody );
401- span .end ();
402- return exporter .getFinishedSpanItems ().get (0 ).getAttributes ().get (INPUT_JSON );
412+ }
413+
414+ @ Test
415+ void openAiResponsesRecognizesDeploymentPrefixedEndpoint () {
416+ String body =
417+ """
418+ {"instructions": "be terse", "input": "hello"}
419+ """ ;
420+ assertEquals (
421+ json (
422+ """
423+ [{"role": "system", "content": "be terse"},
424+ {"role": "user", "content": "hello"}]
425+ """ ),
426+ json (
427+ inputJsonForRequest (
428+ InstrumentationSemConv .PROVIDER_NAME_OPENAI ,
429+ List .of ("openai" , "deployments" , "my-model" , "responses" ),
430+ "POST" ,
431+ body )));
432+ }
433+
434+ @ Test
435+ void openAiResponsesArrayPreservesToolAndMultimodalEntriesAfterInstructions () {
436+ String body =
437+ """
438+ {
439+ "instructions": "be terse",
440+ "input": [
441+ {"role": "user", "content": [
442+ {"type": "input_text", "text": "describe this"},
443+ {"type": "input_image", "image_url": "https://example.com/image.png"}
444+ ]},
445+ {"type": "function_call", "id": "fc_1", "call_id": "call_1",
446+ "name": "get_weather", "arguments": "{\\ "city\\ ":\\ "SF\\ "}"},
447+ {"type": "function_call_output", "call_id": "call_1", "output": "sunny"},
448+ {"type": "item_reference", "id": "msg_previous"}
449+ ]
450+ }
451+ """ ;
452+ JsonNode originalInput = json (body ).get ("input" );
453+ JsonNode captured = json (responsesInput (body ));
454+ assertEquals (originalInput .size () + 1 , captured .size ());
455+ assertEquals (
456+ json (
457+ """
458+ {"role": "system", "content": "be terse"}
459+ """ ),
460+ captured .get (0 ));
461+ for (int i = 0 ; i < originalInput .size (); i ++) {
462+ assertEquals (originalInput .get (i ), captured .get (i + 1 ));
463+ }
464+
465+ var withoutInstructions = json (body ).deepCopy ();
466+ ((com .fasterxml .jackson .databind .node .ObjectNode ) withoutInstructions )
467+ .remove ("instructions" );
468+ assertEquals (originalInput , json (responsesInput (withoutInstructions .toString ())));
469+ }
470+
471+ @ Test
472+ void openAiResponsesInstructionsOnlyContinuationOmitsMissingOrNullInput () {
473+ for (String body :
474+ List .of (
475+ """
476+ {"instructions": "continue tersely", "previous_response_id": "resp_1"}
477+ """ ,
478+ """
479+ {"instructions": "continue tersely", "previous_response_id": "resp_1",
480+ "input": null}
481+ """ )) {
482+ assertEquals (
483+ json (
484+ """
485+ [{"role": "system", "content": "continue tersely"}]
486+ """ ),
487+ json (responsesInput (body )));
488+ }
489+ }
490+
491+ @ Test
492+ void openAiResponsesAbsentOrNullInstructionsDoNotAddSystemMessage () {
493+ JsonNode expected =
494+ json (
495+ """
496+ [{"role": "user", "content": "hello"}]
497+ """ );
498+ for (String body :
499+ List .of (
500+ """
501+ {"input": "hello"}
502+ """ ,
503+ """
504+ {"instructions": null, "input": "hello"}
505+ """ ,
506+ """
507+ {"input": [{"role": "user", "content": "hello"}]}
508+ """ ,
509+ """
510+ {"instructions": null, "input": [{"role": "user", "content": "hello"}]}
511+ """ )) {
512+ assertEquals (expected , json (responsesInput (body )));
513+ }
514+ for (String body :
515+ List .of (
516+ "{}" ,
517+ "{\" instructions\" : null}" ,
518+ "{\" input\" : null}" ,
519+ "{\" instructions\" : null, \" input\" : null}" )) {
520+ assertNull (responsesInput (body ), body );
521+ }
522+ }
523+
524+ @ Test
525+ void openAiResponsesEmptyContentIsPreserved () {
526+ assertEquals (json ("[]" ), json (responsesInput ("{\" input\" : []}" )));
527+ assertEquals (
528+ json (
529+ """
530+ [{"role": "system", "content": ""}, {"role": "user", "content": ""}]
531+ """ ),
532+ json (responsesInput ("{\" instructions\" : \" \" , \" input\" : \" \" }" )));
533+ }
534+
535+ @ Test
536+ void openAiNonResponsesEndpointsDoNotNormalizeInputOrInstructions () {
537+ for (List <String > path :
538+ List .of (
539+ List .<String >of (),
540+ List .of ("v1" , "embeddings" ),
541+ List .of ("v1" , "moderations" ),
542+ List .of ("v1" , "audio" , "speech" ),
543+ List .of ("v1" , "chat" , "completions" ),
544+ List .of ("v1" , "responses" , "resp_1" ),
545+ List .of ("responses" , "batches" ),
546+ List .of ("v1" , "not-responses" ))) {
547+ assertEquals (
548+ json ("[\" hello\" , \" world\" ]" ),
549+ json (
550+ inputJsonForRequest (
551+ InstrumentationSemConv .PROVIDER_NAME_OPENAI ,
552+ path ,
553+ "POST" ,
554+ """
555+ {"instructions": "be terse", "input": ["hello", "world"]}
556+ """ )),
557+ path .toString ());
558+ }
559+ assertNull (
560+ inputJsonForRequest (
561+ InstrumentationSemConv .PROVIDER_NAME_OPENAI ,
562+ List .of ("v1" , "responses" ),
563+ "GET" ,
564+ "{\" instructions\" : \" be terse\" , \" input\" : \" hello\" }" ));
565+ assertEquals (
566+ json (
567+ """
568+ [{"role": "user", "content": "chat message"}]
569+ """ ),
570+ json (
571+ inputJsonForRequest (
572+ InstrumentationSemConv .PROVIDER_NAME_OPENAI ,
573+ List .of ("v1" , "chat" , "completions" ),
574+ "POST" ,
575+ """
576+ {"messages": [{"role": "user", "content": "chat message"}],
577+ "instructions": "be terse", "input": "hello"}
578+ """ )));
403579 }
404580
405581 @ Test
0 commit comments