Skip to content

fix(ollama): declare ResponseError as fallback-eligible so the structured-output ladder can switch strategies - #3615

Closed
konlue wants to merge 1 commit into
agentscope-ai:mainfrom
konlue:fix/ollama-structured-fallback-exceptions
Closed

konlue wants to merge 1 commit into
agentscope-ai:mainfrom
konlue:fix/ollama-structured-fallback-exceptions

Conversation

@konlue

@konlue konlue commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope Version

2.0.12dev (main @ 4acacb9)

Description

OllamaChatModel is the only mainline provider that does not override _get_structured_output_fallback_exceptions (base returns ()). In the structured-output strategy ladder (model/_base.py:565-594), an exception that is neither retryable nor fallback-eligible raises immediately — so for Ollama, any server-side rejection (ollama.ResponseError, which the SDK uses for every non-transport failure including 4xx) kills the whole ladder on the first attempt, and the auto / no_think / none strategies are never tried.

Changes — declare the fallback exception, verbatim provider style:

@classmethod
def _get_structured_output_fallback_exceptions(cls) -> tuple[Type[Exception], ...]:
    from ollama import ResponseError

    # ResponseError wraps every server-side rejection (including 4xx
    # like "model not found" or a tools/structured-output refusal) into
    # a single type, so the structured-output ladder must fall through
    # on it instead of aborting. Transport-level failures stay
    # retryable via the httpx set above.
    return (ResponseError,)

The retryable set (three httpx transport errors) stays untouched — transport failures keep retrying, protocol-level rejections now switch strategies. Same series as #3583/#3588 (Ollama thinking toggle) and #3612/#3613 (Gemini 4xx fall-through).

How to test

Regression cases added to the existing tests/model_ollama_test.py:

  • test_fallback_exceptions_declare_response_error — fails on main (() != (ResponseError,));
  • test_response_error_walks_the_ladder — records every _call_api attempt while the ladder degrades: on main a mocked ResponseError dies after 1 attempt; with this fix all four strategies are tried (4 attempts).
python -m pytest tests/model_ollama_test.py
# 15 passed.

Fixes #3614

Checklist

  • An issue has been created for this PR
  • I have read the CONTRIBUTING.md
  • Docstrings are in Google style
  • Related documentation has been updated (e.g. links, examples, etc.) in documentation repository
  • Code is ready for review

@github-actions github-actions Bot added pr/awaiting-review Waiting for maintainers to review this PR pr/no-linked-issue This PR does not link to any issue labels Oct 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request. One thing worth sorting out before it is reviewed:

  • This pull request does not close an issue. Please open one describing the problem first and link it with Fixes #<number>, so the change can be discussed against the problem it solves.

If you think it belongs here anyway, say so and a maintainer will take a look.

@konlue

konlue commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Withdrawing — see #3614: the premise does not hold for Ollama (the strategies send byte-identical payloads, so a fallback cannot recover). Thanks for the automated check.

@konlue konlue closed this Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/awaiting-review Waiting for maintainers to review this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ollama structured-output ladder aborts on ResponseError — no fallback exceptions declared

1 participant