@@ -692,41 +692,46 @@ private List<LLMObs.LLMMessage> extractInputMessagesForPromptTracking(
692692 }
693693 });
694694
695- if (!messages .isEmpty ()) {
696- return messages ;
697- }
698-
699695 // Fallback for SDK union parsing mismatches: parse raw instructions payload.
700- Optional <JsonValue > rawInstructions = response ._instructions ().asUnknown ();
701- if (rawInstructions .isPresent ()) {
702- Optional <List <JsonValue >> rawList = rawInstructions .get ().asArray ();
703- if (rawList .isPresent ()) {
704- for (JsonValue item : rawList .get ()) {
705- LLMObs .LLMMessage message = extractMessageFromRawInstruction (item );
706- if (message != null ) {
707- messages .add (message );
696+ if (messages .isEmpty ()) {
697+ Optional <JsonValue > rawInstructions = response ._instructions ().asUnknown ();
698+ if (rawInstructions .isPresent ()) {
699+ Optional <List <JsonValue >> rawList = rawInstructions .get ().asArray ();
700+ if (rawList .isPresent ()) {
701+ for (JsonValue item : rawList .get ()) {
702+ LLMObs .LLMMessage message = extractMessageFromRawInstruction (item );
703+ if (message != null ) {
704+ messages .add (message );
705+ }
708706 }
709707 }
710708 }
711709 }
712710
713- if (!messages .isEmpty ()) {
714- return messages ;
715- }
711+ boolean hasInstructions = !messages .isEmpty ();
716712
713+ // Always include input messages from the span tag (set by withResponseCreateParams, which
714+ // records both instructions as "system" and input as "user"). When instructions were already
715+ // collected above, skip "system" messages here to avoid duplicating them.
717716 Object inputTag = span .getTag (CommonTags .INPUT );
718717 if (inputTag instanceof List ) {
719718 for (Object messageObj : (List <?>) inputTag ) {
720719 if (messageObj instanceof LLMObs .LLMMessage ) {
721- messages .add ((LLMObs .LLMMessage ) messageObj );
720+ LLMObs .LLMMessage msg = (LLMObs .LLMMessage ) messageObj ;
721+ if (!hasInstructions || !"system" .equals (msg .getRole ())) {
722+ messages .add (msg );
723+ }
722724 }
723725 }
724726 } else if (inputTag instanceof Map ) {
725727 Object messagesObj = ((Map <?, ?>) inputTag ).get ("messages" );
726728 if (messagesObj instanceof List ) {
727729 for (Object messageObj : (List <?>) messagesObj ) {
728730 if (messageObj instanceof LLMObs .LLMMessage ) {
729- messages .add ((LLMObs .LLMMessage ) messageObj );
731+ LLMObs .LLMMessage msg = (LLMObs .LLMMessage ) messageObj ;
732+ if (!hasInstructions || !"system" .equals (msg .getRole ())) {
733+ messages .add (msg );
734+ }
730735 }
731736 }
732737 }
0 commit comments