-
Notifications
You must be signed in to change notification settings - Fork 56
[opentelemetry-instrumentation-genai-langchain] Divert SystemMessage to gen_ai.system_instructions
#511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sfc-gh-zeningchen
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
sfc-gh-zeningchen:fix/ob-64558-system-instructions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[opentelemetry-instrumentation-genai-langchain] Divert SystemMessage to gen_ai.system_instructions
#511
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ba482b
langchain: divert SystemMessage to gen_ai.system_instructions
sfc-gh-zeningchen 60333ab
langchain: add changelog fragment for #511
sfc-gh-zeningchen 38fb0e3
Merge branch 'main' into fix/ob-64558-system-instructions
sfc-gh-zeningchen 0f35058
langchain: materialize messages before convert_to_messages try/except
sfc-gh-zeningchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
instrumentation/opentelemetry-instrumentation-genai-langchain/.changelog/511.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Divert `SystemMessage` inputs to `gen_ai.system_instructions` instead of emitting them as `role="system"` inside `gen_ai.input.messages`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,15 +7,19 @@ | |
| import pytest | ||
| from langchain_anthropic import ChatAnthropic | ||
| from langchain_core.messages import ( | ||
| AIMessage, | ||
| FunctionMessage, | ||
| HumanMessage, | ||
| SystemMessage, | ||
| SystemMessageChunk, | ||
| ) | ||
| from langchain_core.tools import tool | ||
| from openai import AuthenticationError | ||
|
|
||
| from opentelemetry.instrumentation.genai.langchain.utils import ( | ||
| split_system_and_input_messages, | ||
| to_input_messages, | ||
| to_system_instruction, | ||
| ) | ||
| from opentelemetry.sdk.trace import ReadableSpan | ||
| from opentelemetry.semconv._incubating.attributes import ( | ||
|
|
@@ -406,6 +410,112 @@ def test_function_message_role_maps_to_tool(): | |
| assert result[0].role == "tool" | ||
|
|
||
|
|
||
| def test_split_system_and_input_messages_diverts_system_instructions(): | ||
| system, inputs = split_system_and_input_messages( | ||
| [ | ||
| SystemMessage(content="You are helpful."), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please add system instructions to conformance tests? this is where we validate conformance to semantic conventions including value types |
||
| HumanMessage(content="Hi"), | ||
| ] | ||
| ) | ||
| assert len(system) == 1 | ||
| assert system[0].content == "You are helpful." | ||
| assert system[0].type == "text" | ||
| assert len(inputs) == 1 | ||
| assert inputs[0].role == "user" | ||
|
|
||
|
|
||
| def test_split_system_and_input_messages_position_independent(): | ||
| # A ``SystemMessage`` interleaved between non-system messages still lands | ||
| # in ``system_instruction`` — semconv treats it as a top-level list rather | ||
| # than an interleaved role, and combining multiple system chunks in order | ||
| # matches how providers deliver them. | ||
| system, inputs = split_system_and_input_messages( | ||
| [ | ||
| SystemMessage(content="First guidance."), | ||
| HumanMessage(content="Hi"), | ||
| AIMessage(content="Hello"), | ||
| SystemMessage(content="Second guidance."), | ||
| HumanMessage(content="Follow-up"), | ||
| ] | ||
| ) | ||
| assert [part.content for part in system] == [ | ||
| "First guidance.", | ||
| "Second guidance.", | ||
| ] | ||
| assert [msg.role for msg in inputs] == ["user", "assistant", "user"] | ||
|
|
||
|
|
||
| def test_split_system_and_input_messages_handles_system_message_chunk(): | ||
| # ``SystemMessageChunk`` (streaming) subclasses ``SystemMessage`` — the | ||
| # helper matches by ``isinstance`` so streamed system content is diverted | ||
| # too instead of leaking into ``gen_ai.input.messages``. | ||
| system, inputs = split_system_and_input_messages( | ||
| [ | ||
| SystemMessageChunk(content="Streamed system prompt."), | ||
| HumanMessage(content="Hi"), | ||
| ] | ||
| ) | ||
| assert len(system) == 1 | ||
| assert system[0].content == "Streamed system prompt." | ||
| assert [msg.role for msg in inputs] == ["user"] | ||
|
|
||
|
|
||
| def test_split_system_and_input_messages_empty_when_no_system_message(): | ||
| system, inputs = split_system_and_input_messages( | ||
| [HumanMessage(content="Hi")] | ||
| ) | ||
| assert system == [] | ||
| assert [msg.role for msg in inputs] == ["user"] | ||
|
|
||
|
|
||
| def test_split_system_and_input_messages_normalizes_shorthand_inputs(): | ||
| # Short-hand tuple / dict forms are normalized via ``convert_to_messages`` | ||
| # before the ``isinstance`` partition so that a short-hand system entry is | ||
| # diverted to ``system_instruction`` instead of leaking into | ||
| # ``gen_ai.input.messages`` as ``role: "system"``. | ||
| system, inputs = split_system_and_input_messages( | ||
| [ | ||
| ("system", "You are helpful."), | ||
| {"role": "user", "content": "Hi"}, | ||
| ] | ||
| ) | ||
| assert [part.content for part in system] == ["You are helpful."] | ||
| assert [msg.role for msg in inputs] == ["user"] | ||
|
|
||
|
|
||
| def test_to_system_instruction_ignores_non_system_messages(): | ||
| # Symmetric to ``to_input_messages``: ``to_system_instruction`` only | ||
| # emits parts from ``SystemMessage`` s and silently drops everything else. | ||
| parts = to_system_instruction( | ||
| [ | ||
| HumanMessage(content="Hi"), | ||
| SystemMessage(content="You are helpful."), | ||
| AIMessage(content="Hello"), | ||
| SystemMessageChunk(content="More guidance."), | ||
| ] | ||
| ) | ||
| assert [part.content for part in parts] == [ | ||
| "You are helpful.", | ||
| "More guidance.", | ||
| ] | ||
|
|
||
|
|
||
| def test_to_system_instruction_normalizes_shorthand_inputs(): | ||
| # Accepts ``("system", "…")`` tuples and ``{"role": "system", …}`` dicts | ||
| # via ``convert_to_messages``, matching ``to_input_messages`` behavior. | ||
| parts = to_system_instruction( | ||
| [ | ||
| ("system", "First guidance."), | ||
| {"role": "user", "content": "Hi"}, | ||
| {"role": "system", "content": "Second guidance."}, | ||
| ] | ||
| ) | ||
| assert [part.content for part in parts] == [ | ||
| "First guidance.", | ||
| "Second guidance.", | ||
| ] | ||
|
|
||
|
|
||
| def assert_openai_completion_attributes( | ||
| span: ReadableSpan, response: Optional, verify_content: bool = True | ||
| ): | ||
|
|
@@ -469,11 +579,18 @@ def assert_openai_completion_attributes( | |
| if verify_content: | ||
| input_message = attributes[gen_ai_attributes.GEN_AI_INPUT_MESSAGES] | ||
| assert input_message is not None | ||
| assert '"role":"system"' in input_message | ||
| assert '"content":"You are a helpful assistant!"' in input_message | ||
| assert '"role":"system"' not in input_message | ||
| assert '"role":"user"' in input_message | ||
| assert '"content":"What is the capital of France?"' in input_message | ||
|
|
||
| system_instructions = attributes[ | ||
| gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS | ||
| ] | ||
| assert system_instructions is not None | ||
| assert ( | ||
| '"content":"You are a helpful assistant!"' in system_instructions | ||
| ) | ||
|
|
||
| # Assert output message | ||
| output_message = attributes[gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES] | ||
| assert output_message is not None | ||
|
|
@@ -483,6 +600,7 @@ def assert_openai_completion_attributes( | |
| else: | ||
| assert gen_ai_attributes.GEN_AI_INPUT_MESSAGES not in attributes | ||
| assert gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES not in attributes | ||
| assert gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS not in attributes | ||
|
|
||
|
|
||
| def assert_openai_completion_attributes_with_error( | ||
|
|
@@ -519,16 +637,24 @@ def assert_openai_completion_attributes_with_error( | |
| if verify_content: | ||
| input_message = attributes[gen_ai_attributes.GEN_AI_INPUT_MESSAGES] | ||
| assert input_message is not None | ||
| assert '"role":"system"' in input_message | ||
| assert '"content":"You are a helpful assistant!"' in input_message | ||
| assert '"role":"system"' not in input_message | ||
| assert '"role":"user"' in input_message | ||
| assert '"content":"What is the capital of France?"' in input_message | ||
|
|
||
| system_instructions = attributes[ | ||
| gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS | ||
| ] | ||
| assert system_instructions is not None | ||
| assert ( | ||
| '"content":"You are a helpful assistant!"' in system_instructions | ||
| ) | ||
|
|
||
| # Assert output message | ||
| assert gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES not in attributes | ||
| else: | ||
| assert gen_ai_attributes.GEN_AI_INPUT_MESSAGES not in attributes | ||
| assert gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES not in attributes | ||
| assert gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS not in attributes | ||
|
|
||
|
|
||
| def assert_bedrock_completion_attributes( | ||
|
|
@@ -768,25 +894,26 @@ def assert_log_record(log_record, parent_span, response=None): | |
| attrs.get(gen_ai_attributes.GEN_AI_INPUT_MESSAGES, []) | ||
| ) | ||
| expected_input = [ | ||
| { | ||
| "parts": [ | ||
| {"content": "You are a helpful assistant!", "type": "text"} | ||
| ], | ||
| "role": "system", | ||
| }, | ||
| { | ||
| "parts": [ | ||
| {"content": "What is the capital of France?", "type": "text"} | ||
| ], | ||
| "role": "user", | ||
| }, | ||
| ] | ||
| assert len(input_msgs) == 2 | ||
| assert len(input_msgs) == 1 | ||
| for i, exp in enumerate(expected_input): | ||
| got = _normalize_to_dict(input_msgs[i]) | ||
| assert got["role"] == exp["role"] | ||
| assert _normalize_to_list(got["parts"]) == exp["parts"] | ||
|
|
||
| system_instructions = _normalize_to_list( | ||
| attrs.get(gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS, []) | ||
| ) | ||
| assert system_instructions == [ | ||
| {"content": "You are a helpful assistant!", "type": "text"} | ||
| ] | ||
|
|
||
| output_msgs = _normalize_to_list( | ||
| attrs.get(gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES, []) | ||
| ) | ||
|
|
@@ -832,25 +959,26 @@ def assert_log_record_when_error(log_record, parent_span): | |
| attrs.get(gen_ai_attributes.GEN_AI_INPUT_MESSAGES, []) | ||
| ) | ||
| expected_input = [ | ||
| { | ||
| "parts": [ | ||
| {"content": "You are a helpful assistant!", "type": "text"} | ||
| ], | ||
| "role": "system", | ||
| }, | ||
| { | ||
| "parts": [ | ||
| {"content": "What is the capital of France?", "type": "text"} | ||
| ], | ||
| "role": "user", | ||
| }, | ||
| ] | ||
| assert len(input_msgs) == 2 | ||
| assert len(input_msgs) == 1 | ||
| for i, exp in enumerate(expected_input): | ||
| got = _normalize_to_dict(input_msgs[i]) | ||
| assert got["role"] == exp["role"] | ||
| assert _normalize_to_list(got["parts"]) == exp["parts"] | ||
|
|
||
| system_instructions = _normalize_to_list( | ||
| attrs.get(gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS, []) | ||
| ) | ||
| assert system_instructions == [ | ||
| {"content": "You are a helpful assistant!", "type": "text"} | ||
| ] | ||
|
|
||
| assert gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES not in attrs | ||
| assert_log_parent(log_record, parent_span) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This processes the normalized messages in a single pass instead of splitting them and calling
to_system_instructionandto_input_messages, which each re-runconvert_to_messagesand iterate the list again.