Skip to content

Do not record the unset options of a spring-ai-1.x streaming chat request as the string null - #854

Open
1chengfengyu wants to merge 1 commit into
apache:mainfrom
1chengfengyu:fix/spring-ai-stream-null-tags
Open

1chengfengyu wants to merge 1 commit into
apache:mainfrom
1chengfengyu:fix/spring-ai-stream-null-tags

Conversation

@1chengfengyu

Copy link
Copy Markdown
Contributor

Fix the spring-ai-1.x plugin recording the string null as the value of a request option a streaming call leaves unset

  • Add a unit test to verify that the fix works.
  • Explain briefly why the bug exists and how to fix it.

Why the bug exists

ChatModelStreamInterceptor writes four request options without checking them first
(apm-spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java,
lines 74-77 as of 6974276):

Tags.GEN_AI_REQUEST_MODEL.set(span, chatOptions.getModel());
Tags.GEN_AI_TEMPERATURE.set(span, String.valueOf(chatOptions.getTemperature()));
Tags.GEN_AI_TOP_K.set(span, String.valueOf(chatOptions.getTopK()));
Tags.GEN_AI_TOP_P.set(span, String.valueOf(chatOptions.getTopP()));

ChatOptions returns null for every sampling setting a request does not carry, and String.valueOf turns that
null into the four-character string null. The collector has no way to tell it from a value the application really
set, so the tag is stored as is.

gen_ai.request.top_k is the one that fires in practice. The OpenAI API has no top_k parameter, so
OpenAiChatOptions.getTopK() is null unless the application sets it explicitly, which means the span of every
OpenAI streaming call carries gen_ai.request.top_k=null.

The non-streaming interceptor guards all four with != null already, so one request recorded two different tag sets
depending on whether the application called call() or stream(). For the same prompt with only model,
temperature and top-p configured, the E2E scenario shows:

Spring-ai/openai/call                    Spring-ai/openai/stream
  gen_ai.request.model       gpt-4.1      gen_ai.request.model       gpt-4.1
  gen_ai.request.temperature 0.7          gen_ai.request.temperature 0.7
  gen_ai.request.top_p       0.9          gen_ai.request.top_k       'null'   <- string, not the number
                                          gen_ai.request.top_p       0.9

Why the E2E scenario did not catch it

The expected tags of the streaming span contained

- { key: gen_ai.request.top_k, value: null }

The validator reads that as a YAML null, and ExpressParser.parse(null) returns NoopAssertor, so the value was
never compared. The line only required that a tag with that key exists at that position, and "null" satisfied it.

How it is fixed

Each of the four options is written only when the request carries it, which is what the non-streaming interceptor and
the OpenTelemetry GenAI conventions both do. The expected tag is removed with it, because SegmentAssert.tagsEquals
compares the two tag lists by size and position: the expected list now has 17 entries, so a gen_ai.request.top_k
that comes back fails the scenario instead of passing silently.

Verification

bash test/plugin/run.sh --debug spring-ai-1.x-scenario on both supported versions:

Scenarios: spring-ai-1.x-scenario, Testcases: 2, Elapsed: 00:01:03

Tag lists of Spring-ai/openai/stream from workspace/spring-ai-1.x-scenario/1.1.1/data/actualData.yaml, before and
after the change:

       tags:
       - {key: gen_ai.operation.name, value: chat}
       - {key: gen_ai.provider.name, value: openai}
       - {key: gen_ai.request.model, value: gpt-4.1-2025-04-14}
       - {key: gen_ai.request.temperature, value: '0.7'}
-      - {key: gen_ai.request.top_k, value: 'null'}
       - {key: gen_ai.request.top_p, value: '0.9'}
       - {key: gen_ai.request.max_tokens, value: '1000'}
       - {key: gen_ai.request.frequency_penalty, value: '0.5'}
       - {key: gen_ai.request.presence_penalty, value: '0.3'}

No value: 'null' is left in either actualData.yaml.

@wu-sheng wu-sheng added this to the 9.9.0 milestone Dec 3, 2026
@wu-sheng wu-sheng added the bug Something isn't working label Dec 3, 2026
@wu-sheng
wu-sheng requested a review from peachisai December 3, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants