fix(model): prevent NPE when GenerateOptions is null in ChatModelBase…#1667
fix(model): prevent NPE when GenerateOptions is null in ChatModelBase…#1667hangweizhang wants to merge 1 commit into
Conversation
….stream() When HarnessAgent.streamEvents() is called without explicitly setting generateOptions, the options field remains null and gets passed down to ChatModelBase.stream() and eventually to doStream() implementations. OllamaChatModel.doStream() directly called options.getToolChoice() without a null-check, causing a NullPointerException: Cannot invoke "GenerateOptions.getToolChoice()" because "options" is null The non-streaming call() path was not affected because ReActAgent.buildGenerateOptions() already falls back to GenerateOptions.builder().build() when generateOptions is null. Fix: - ChatModelBase.stream(): replace null options with GenerateOptions.builder().build() before delegating to doStream(), ensuring all subclass implementations receive non-null options - OllamaChatModel.doStream(): add defensive null-check as well, in case doStream is called through other code paths - Add unit tests verifying null options are handled safely Closes agentscope-ai#1644
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@LearningGp fix for NPE #1667 please review thx |
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
This PR fixes a NullPointerException in the streaming path when GenerateOptions is null — specifically when HarnessAgent.streamEvents() is called without explicitly setting generate options. The fix is applied at two levels: (1) ChatModelBase.stream() replaces null with a default GenerateOptions instance before delegating to doStream() and the tracer, which is the correct abstraction level to centralize this guard; (2) OllamaChatModel.doStream() adds a defensive null-check as belt-and-suspenders for potential direct invocations. The included unit tests cover null, default-replacement, and passthrough scenarios using a clean StubModel. The changes are minimal, focused, and well-aligned with the project's existing patterns. Closes #1644.
….stream()
When HarnessAgent.streamEvents() is called without explicitly setting generateOptions, the options field remains null and gets passed down to ChatModelBase.stream() and eventually to doStream() implementations. OllamaChatModel.doStream() directly called options.getToolChoice() without a null-check, causing a NullPointerException:
Cannot invoke "GenerateOptions.getToolChoice()" because "options" is null
The non-streaming call() path was not affected because ReActAgent.buildGenerateOptions() already falls back to GenerateOptions.builder().build() when generateOptions is null.
Fix:
Closes #1644
AgentScope-Java Version
[The version of AgentScope-Java you are working on, e.g. 1.0.12, check your pom.xml dependency version or run
mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]Description
[Please describe the background, purpose, changes made, and how to test this PR]
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)