Skip to content

Conversation

@hangweizhang
Copy link

Fix: Trace does not record temperature parameter in span attributes (#767)

Problem

Using AgentScope-Java with TelemetryTracer (e.g. Langfuse/Jaeger), gen_ai.request.temperature and related attributes were not recorded in trace spans, even though the OpenTelemetry GenAI semantic conventions define them.

Root Cause

  • ReActAgent.buildGenerateOptions() only set executionConfig (timeout/retry), and did not propagate generation options such as temperature, topP, and maxTokens
  • ReActAgent.Builder had no way to configure GenerateOptions, so these parameters could not be set at agent level
  • AttributesExtractors.getLLMRequestAttributes() only records attributes when the options are non-null, so temperature stayed missing in traces

Solution

  1. Add generateOptions(GenerateOptions) to ReActAgent.Builder so users can configure generation options (temperature, topP, maxTokens, etc.) at agent creation.
  2. Update buildGenerateOptions() to merge user-provided generateOptions with modelExecutionConfig via GenerateOptions.mergeOptions().
  3. Update notifyPreReasoningEvent() so the event receives the effective options from buildGenerateOptions(), enabling hooks and tracing to see the full configuration.

Usage Example

ReActAgent agent = ReActAgent.builder()
.name("TestAgent")
.model(chatModel)
.generateOptions(GenerateOptions.builder()
.temperature(0.7)
.topP(0.9)
.maxTokens(1000)
.build())
.build();

agent.call(Msg.userMsg("Hello")).block();

- 在ReActAgent中添加generateOptions字段,用于配置生成参数(温度、topP、最大token等)
- 更新buildGenerateOptions方法,将modelExecutionConfig与generateOptions合并
- 在预推理事件通知中包含生成选项以丰富事件信息
- 在Builder中新增generateOptions设置方法,支持链式调用
- 添加单元测试,验证生成选项正确传递给模型并在调用中生效
@hangweizhang hangweizhang requested a review from a team February 10, 2026 03:48
@cla-assistant
Copy link

cla-assistant bot commented Feb 10, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zhanghangwei seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant
Copy link

cla-assistant bot commented Feb 10, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zhanghangwei seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gemini-code-assist
Copy link

Summary of Changes

Hello @hangweizhang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where LLM generation parameters, such as temperature and topP, were not being recorded in trace spans when using AgentScope-Java with tracing tools like Langfuse or Jaeger. The changes enable the ReActAgent to accept and propagate these GenerateOptions throughout its lifecycle, ensuring that all relevant model configuration details are available for tracing and monitoring.

Highlights

  • Agent Configuration: Introduced a generateOptions method to ReActAgent.Builder allowing users to configure LLM generation parameters like temperature, topP, and maxTokens at agent creation.
  • Option Merging Logic: Modified ReActAgent.buildGenerateOptions() to correctly merge user-provided generateOptions with existing modelExecutionConfig, ensuring all relevant parameters are considered.
  • Tracing Enhancement: Updated notifyPreReasoningEvent() to pass the fully merged GenerateOptions to event hooks, enabling proper recording of generation parameters in trace spans (e.g., for OpenTelemetry GenAI semantic conventions).
Changelog
  • agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
    • Added a generateOptions field to the ReActAgent class.
    • Updated the ReActAgent constructor to initialize the new generateOptions field.
    • Refactored buildGenerateOptions to merge modelExecutionConfig with the newly introduced generateOptions.
    • Modified notifyPreReasoningEvent to pass the complete GenerateOptions obtained from buildGenerateOptions.
    • Introduced a generateOptions field and a corresponding builder method in ReActAgent.Builder for configuring generation parameters.
  • agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentTest.java
    • Added an import statement for GenerateOptions.
    • Implemented a new test case, testGenerateOptionsPassedToModel, to verify that GenerateOptions (temperature, topP, maxTokens) are correctly passed to the mock model.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 Pull Request 很好地解决了在追踪(tracing)中 GenerateOptions(如 temperature)未能传递给模型的问题。代码变更逻辑清晰,通过在 ReActAgent.Builder 中添加 generateOptions,并在 buildGenerateOptions 方法中合并选项,正确地实现了功能。同时,还添加了相应的单元测试来验证新功能。我提出了一些关于代码可读性和测试健壮性的建议,但总体来说,这是一个高质量的提交。

Comment on lines 790 to 799
protected GenerateOptions buildGenerateOptions() {
GenerateOptions.Builder builder = GenerateOptions.builder();
if (modelExecutionConfig != null) {
builder.executionConfig(modelExecutionConfig);
}
return builder.build();
// Build options with modelExecutionConfig (timeout, retry, etc.)
GenerateOptions executionOptions =
modelExecutionConfig != null
? GenerateOptions.builder().executionConfig(modelExecutionConfig).build()
: null;
// Merge: user's generateOptions (temperature, topP, maxTokens, etc.) as base,
// executionOptions overrides executionConfig
return GenerateOptions.mergeOptions(executionOptions, generateOptions);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

当前的实现虽然功能正确,但使用三元运算符使代码略显紧凑,可读性稍差。通过提前处理 modelExecutionConfig == null 的情况,可以使逻辑更清晰:如果没有特定于 agent 的执行配置,就直接使用 generateOptions;否则,将 agent 的配置合并到基础选项之上。

Suggested change
protected GenerateOptions buildGenerateOptions() {
GenerateOptions.Builder builder = GenerateOptions.builder();
if (modelExecutionConfig != null) {
builder.executionConfig(modelExecutionConfig);
}
return builder.build();
// Build options with modelExecutionConfig (timeout, retry, etc.)
GenerateOptions executionOptions =
modelExecutionConfig != null
? GenerateOptions.builder().executionConfig(modelExecutionConfig).build()
: null;
// Merge: user's generateOptions (temperature, topP, maxTokens, etc.) as base,
// executionOptions overrides executionConfig
return GenerateOptions.mergeOptions(executionOptions, generateOptions);
}
protected GenerateOptions buildGenerateOptions() {
if (modelExecutionConfig == null) {
return generateOptions;
}
// The agent's modelExecutionConfig should override any execution config
// within the provided generateOptions.
GenerateOptions executionOptions =
GenerateOptions.builder().executionConfig(modelExecutionConfig).build();
return GenerateOptions.mergeOptions(executionOptions, generateOptions);
}

Comment on lines +128 to +129
assertEquals(0.7, lastOptions.getTemperature(), "Temperature should be passed to model");
assertEquals(0.9, lastOptions.getTopP(), "TopP should be passed to model");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在比较浮点数时,使用一个容差(delta)是最佳实践,可以避免因精度问题导致的测试失败。虽然在这里使用字面量比较可能不会出问题,但使用带 delta 的 assertEquals 会使测试更加健壮。

Suggested change
assertEquals(0.7, lastOptions.getTemperature(), "Temperature should be passed to model");
assertEquals(0.9, lastOptions.getTopP(), "TopP should be passed to model");
assertEquals(0.7, lastOptions.getTemperature(), 0.0, "Temperature should be passed to model");
assertEquals(0.9, lastOptions.getTopP(), 0.0, "TopP should be passed to model");

@codecov
Copy link

codecov bot commented Feb 10, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 77.77% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant