spring-projects / spring-projects/spring-ai

OpenAiChatModel observation lost response data for stream calling

Open
#3,875 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
When tracing is enabled and the OpenAiChatModel is invoked using the stream method, the Response metric data at the Model level cannot be obtained in the ObservationHandler processing class

Environment
The latest Spring ai version and oracle jdk21

Steps to reproduce

    1. Through debugging, it was found that there may be some issues with the following code:
.doFinally(s -> observation.stop())
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));
// @formatter:on

return new MessageAggregator().aggregate(flux, observationContext::setResponse);

This code may cause observation.stop() to run before observationContext::setResponse, which may lead to issues when DefaultChatClientObservationConvention generates metrics,
Unable to obtain the metric data from the server response because the response is empty

    1. After the following modifications, it works OK
.doOnError(observation::error)
//.doFinally(s -> observation.stop());
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));
// @formatter:on

return new MessageAggregator().aggregate(flux, observationContext::setResponse)
                    .doFinally(s -> observation.stop());

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the OpenAiChatModel stream path and trace how MessageAggregator sets the response relative to observation.stop(). Reproduce the streaming call with tracing enabled and verify that the ObservationHandler receives the server response data before the observation is stopped.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
observability
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.