spring-projects / spring-projects/spring-ai
OpenAiChatModel observation lost response data for stream calling
Nobody has claimed this yet.
- 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
-
- 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
-
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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