Skip to content

Fix callbacks for synchronous model errors - #1649

Open
codingkiddo wants to merge 1 commit into
google:mainfrom
codingkiddo:fix/synchronous-model-error-callbacks
Open

codingkiddo wants to merge 1 commit into
google:mainfrom
codingkiddo:fix/synchronous-model-error-callbacks

Conversation

@codingkiddo

Copy link
Copy Markdown

Description of Change

Problem:

BaseLlmFlow invokes generateContent() before attaching its model-error
handler. If a model throws synchronously before returning a Flowable,
the exception bypasses onModelErrorCallback and downstream error
span recording.

The existing outer Flowable.defer emits the exception outside the
inner error-handling chain.

Solution:

Wrap generateContent() in Flowable.defer so synchronous exceptions
reach the existing model-error handling.

Callbacks can provide a fallback response. If no fallback is provided,
the original exception propagates through the existing error-recording
path. The fix applies across model implementations.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added regression tests covering:

  • Recovery from a synchronous model exception using a callback-provided response.
  • Invocation of the callback and propagation of the original exception
    when the callback returns empty.

Validation command:

./mvnw -pl core -am \
  '-Dtest=BaseLlmFlowTest#run_synchronousModelError*' \
  -Dsurefire.failIfNoSpecifiedTests=false \
  test

Result: 2 tests run, 0 failures, 0 errors, 0 skipped.

Manual End-to-End (E2E) Tests:

Ran a standalone Maven application using LlmAgent, InMemoryRunner,
and a custom Ollama-backed BaseLlm adapter against local Ollama.

Requested a nonexistent model to trigger HTTP 404. The adapter performs
the HTTP request eagerly and throws synchronously before returning
its Flowable.

With the fixed SDK, onModelErrorCallback received the exception exactly
once, and the caller received the callback's fallback response.

Command:

mvn compile exec:java -Dexec.args="adk-error-demo-missing-model"

Observed output:

CALLBACK: Ollama HTTP 404: {"error":"model 'adk-error-demo-missing-model' not found"}
RESPONSE: Model unavailable; fallback returned.
CALLBACK COUNT: 1
BUILD SUCCESS

A separate SDK run using a simulated synchronous model failure reproduced
the original behavior: the exception escaped and the callback count was zero.

Checklist

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The regression tests require no external API credentials or Ollama server.

The manual demonstration uses a custom eager Ollama adapter to exercise
synchronous model failures; it does not establish a defect in a built-in
Ollama integration.

No dependent module changes are required.

@hemasekhar-p hemasekhar-p self-assigned this Oct 13, 2026
@hemasekhar-p

Copy link
Copy Markdown
Contributor

Hi @codingkiddo, thank you for your contribution! We appreciate you taking the time to submit this pull request. As per contribution policy, your PR consists of a single commit. To proceed with review, Could you please change your commits accordingly?

@hemasekhar-p hemasekhar-p added the waiting on reporter Waiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale. label Oct 13, 2026
Signed-off-by: Vinod Kumar <codingkiddo@gmail.com>
@codingkiddo
codingkiddo force-pushed the fix/synchronous-model-error-callbacks branch from 7eab695 to 697dfbe Compare October 13, 2026 09:21
@codingkiddo

Copy link
Copy Markdown
Author

Hi @codingkiddo, thank you for your contribution! We appreciate you taking the time to submit this pull request. As per contribution policy, your PR consists of a single commit. To proceed with review, Could you please change your commits accordingly?

Hi @hemasekhar-p - Thanks for the review. I’ve squashed the changes into a single signed-off commit (697dfbe) and updated the PR.

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

Labels

waiting on reporter Waiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants