Skip to content

fix(ollama): finish tool calls before native structured output - #3516

Open
guslegend0510 wants to merge 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/ollama-two-phase-structured-output
Open

guslegend0510 wants to merge 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/ollama-two-phase-structured-output

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT

Description

Follow-up to #3455; refs #3452.

Live verification with Ollama 0.40.3 and qwen3:0.6b showed that combining tools and format could suppress a valid weather tool call. A parseable JSON response alone did not prove that the business tool had run.

Enable native structured output with application tools through two phases inside the Ollama provider. Tool reasoning requests omit format, including a configured default format. Tool calls return to the ordinary ReAct loop. When a round returns an answer without tool calls, keep it as an internal draft and make a separate request with the schema and without tools or tool_choice. Only the final structured response reaches the caller; its usage includes the hidden draft.

Tool-phase streaming is buffered until the provider can decide whether to execute tools or format the answer. The terminal logical model round therefore makes two physical HTTP requests. Formatting retries do not replay tools. Native failures with application tools persist completed tool state and propagate the error, preventing synthetic fallback from repeating business side effects. Calls without application tools retain their existing fallback.

Preserve both builder switches and expose them through ModelCreationContext. nativeStructuredOutputWithTools(false) selects the synthetic path when tools are present; nativeStructuredOutput(false) disables native routing entirely. Update the English and Chinese Ollama guides.

Validation

  • Repository-wide mvn -B -T1 clean verify: all 97 reactor modules passed; 10,558 tests, zero failures/errors, 197 skipped.
  • Full Ollama suite: 199 tests, zero failures/errors, 1 conditional skip.
  • Regression coverage: streaming/non-streaming, actual Java tool execution and arguments, multiple tool rounds, default-format removal, hidden draft text/thinking and usage, empty responses, cancellation, failures, retryable formatting errors without tool replay, persisted tool state, and routing switches.
  • Live Java acceptance on Ollama 0.40.3 + qwen3:0.6b passed in both modes: one actual get_weather(city="Paris") execution; three requests (tools-only, tools-only, format-only); final {"city":"Paris","temperature":17.43}; successful getStructuredData; hidden-draft usage accounted for.
  • Live validation is limited to that server/model pair; qwen3.5:9b was not run.
  • Documentation: npm test (13 passed), npm run validate, and npm run broken-links passed.
  • Spotless and git diff --check passed.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All required tests are passing; conditional skips are reported above
  • Javadoc comments follow project conventions
  • Related documentation has been updated
  • Code is ready for review

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...scope/extensions/model/ollama/OllamaChatModel.java 89.87% 1 Missing and 7 partials ⚠️
...e/src/main/java/io/agentscope/core/ReActAgent.java 85.71% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

2 participants