Skip to content

[spring-ai] Wrapping ChatModels without getOptions() override fail with ClassCastException when ADK tools are attached #1731

Description

@caps-xia

Describe the Bug

When the configured ChatModel is a delegating/wrapping implementation that does not override getOptions() (the interface default returns an empty DefaultChatOptions), any ADK request with tools fails with a bare ClassCastException deep inside the provider model.

Chain:

  1. SpringAI.resolveDefaultOptions() returns the model's getOptions() — for wrappers, the interface default: an empty DefaultChatOptions, not null, so the existing null handling never engages;
  2. MessageConverter.buildChatOptions(...) sees defaults that are not ToolCallingChatOptions and falls into the hasTools branch, building provider-neutral ToolCallingChatOptions;
  3. Provider models that cast Prompt.getOptions() to their own type — e.g. OpenAiChatModel.createRequest(...) casting to OpenAiChatOptions — throw ClassCastException: DefaultToolCallingChatOptions cannot be cast to OpenAiChatOptions.

The failure surfaces several frames away from the cause and gives no hint that the wrapper's getOptions() is the origin. We hit this in production with a failover wrapper (2026-09-28): the non-streaming fallback path died on its first call.

Note the related hardening in resolveDefaultOptions() (avoiding the same cast by using the model's own options) covers the "options available" half; this report is the "defaults are the empty interface default" half.

To Reproduce

// Any delegating wrapper that forgets to override getOptions()
class ForgettingWrapper implements ChatModel, StreamingChatModel {
  private final ChatModel delegate;
  // call()/stream() delegate; getOptions() intentionally NOT overridden
}

SpringAI springAI = new SpringAI(new ForgettingWrapper(openAiChatModel));
springAI.generateContent(
    LlmRequest.builder()
        .contents(List.of(userContent("hello")))
        .tools(Map.of("ping", pingTool))   // any ADK tool
        .build(),
    false);
// -> RuntimeException wrapping ClassCastException:
//    DefaultToolCallingChatOptions cannot be cast to org.springframework.ai.openai.OpenAiChatOptions

(Verified with a probe test on current main: the wrapper's getOptions() returns DefaultChatOptions@..., instanceof ToolCallingChatOptions is false, and the cast fails exactly as above.)

Expected behavior

At minimum, an actionable warning/error at the bridge level naming the actual cause ("the wrapping model's getOptions() returns non-provider options; delegate it to the underlying model's options"), instead of a bare ClassCastException from inside the provider. Fail-fast would also be reasonable, except that dummy/test models legitimately use the same default — so we prototyped a WARN in MessageConverter.buildChatOptions (branch: main...caps-xia:adk-java:fix/wrapper-chatmodel-options-cce, 307 tests green) and are happy to open a PR if the direction sounds right.

Environment

  • google/adk-java main (post the resolveDefaultOptions hardening)
  • Spring AI 2.0.1, spring-ai-openai

Additional context

Wrapping/decorating ChatModels (failover, retry, caching, audit) are a common Spring AI pattern; any of them without an explicit getOptions() override hits this the moment ADK tools are attached. A one-line doc note in the SpringAI README ("wrappers must delegate getOptions()") would also help.

Activity

  1. self-assigned this
    on Oct 30, 2026
  2. added theissue type on Nov 5, 2026
  3. hemasekhar-p commented on Nov 5, 2026

    @hemasekhar-p
    Contributor

    Hi @caps-xia, thank you for taking the time to report this issue. I have successfully reproduced this behavior using your inputs. We truly appreciate and welcomes your contribution. Would you be able to provide a pull request to address this issue?

  4. added
    waiting on reporterWaiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale.
    on Nov 5, 2026
  5. caps-xia commented on Nov 6, 2026

    @caps-xia
    ContributorAuthor

    Thanks @hemasekhar-p! PR opened: #1771

  6. hemasekhar-p commented on Nov 9, 2026

    @hemasekhar-p
    Contributor

    Thank you for the fix @caps-xia, Currently this issue and PR are under review by our team, we will keep you posted if any additional information is required. thank you.

  7. added and removed
    waiting on reporterWaiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale.
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions