Skip to content

fix(model): prevent NPE when GenerateOptions is null in ChatModelBase…#1667

Open
hangweizhang wants to merge 1 commit into
agentscope-ai:mainfrom
hangweizhang:fix/issue-1644-stream-events-npe
Open

fix(model): prevent NPE when GenerateOptions is null in ChatModelBase…#1667
hangweizhang wants to merge 1 commit into
agentscope-ai:mainfrom
hangweizhang:fix/issue-1644-stream-events-npe

Conversation

@hangweizhang

Copy link
Copy Markdown
Contributor

….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 #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.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

….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
@hangweizhang hangweizhang requested a review from a team June 7, 2026 03:10
@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...java/io/agentscope/core/model/OllamaChatModel.java 66.66% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@hangweizhang

Copy link
Copy Markdown
Contributor Author

@LearningGp fix for NPE #1667 please review thx

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/model Model providers and formatters labels Jun 7, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 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.

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

Labels

area/core/model Model providers and formatters bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] streamEvents() throws NPE when generateOptions is not set on HarnessAgent

2 participants