#3021·ragas

llm_factory: `mode=` is silently discarded for provider='anthropic' and provider='google'/'gemini' (new google-genai)

Author: BlueX888Created Sep 19, 2026Updated Sep 19, 2026
  • I have checked the documentation and related resources and couldn't resolve my bug.

Describe the bug

llm_factory(..., mode=...) documents mode as the instructor mode for structured output (default Mode.JSON), and _get_instructor_client() does normalize mode to Mode.JSON when it is None. But that normalized value is only forwarded to some branches: instructor.from_openai(client, mode=mode) (line 590), instructor.from_litellm(client, mode=mode) (line 599), and _patch_client_for_provider(client, provider_lower, mode=mode) (line 603). The anthropic and google branches call instructor.from_anthropic(client) (line 592), instructor.from_genai(client) (line 595) and instructor.from_gemini(client) (line 597) with no mode argument at all, so the requested mode is accepted and then silently discarded.

Both affected providers reach this function by default: auto_detect_adapter() routes provider="anthropic" to the instructor adapter (default branch), and provider="google"/"gemini" with a new google.genai.Client to the instructor adapter as well (src/ragas/llms/adapters/__init__.py). The patched client therefore ends up on instructor's own per-factory default (Mode.TOOLS for from_anthropic and from_genai, Mode.MD_JSON for from_gemini) instead of the requested — and documented-default — Mode.JSON. provider="perplexity" (line 601) drops it the same way.

Ragas version: 0.4.4.dev8+g298b68274 Python version: 3.13.15 Commit: 298b68274234c060deacab3cf5fb52aa3a20e885 OS: macOS 26.6.2 (arm64), instructor 1.17.0

Code to Reproduce

Run against the commit above (google-genai/anthropic need not be installed; the real instructor v2 factories are bound directly, which is what instructor's lazy __getattr__ exposes when the SDK is importable):

python
"""Minimal repro: llm_factory(mode=...) is dropped for anthropic / new google-genai."""
import inspect
from unittest.mock import Mock

import instructor
from instructor.v2.providers.anthropic.client import from_anthropic
from instructor.v2.providers.genai.client import from_genai
from instructor.v2.providers.openai.client import from_openai

for n, f in (("from_anthropic", from_anthropic), ("from_genai", from_genai),
             ("from_openai", from_openai)):
    setattr(instructor, n, f)

from ragas.llms.base import _get_instructor_client

FACTORIES = {"from_anthropic": from_anthropic, "from_genai": from_genai,
             "from_openai": from_openai}


class NewGenAIClient:               # google.genai.client.Client (new SDK)
    __module__ = "google.genai.client"
    models = Mock()


class AnthropicClient:              # anthropic.Anthropic
    def __init__(self):
        self.messages = Mock()
        self.messages.create = Mock()


seen = {}


def record(name):
    sig = inspect.signature(FACTORIES[name])
    default = sig.parameters["mode"].default

    def rec(*a, **k):
        seen[name] = sig.bind_partial(*a, **k).arguments.get("mode", default)
        return Mock()

    return rec


for n in FACTORIES:
    setattr(instructor, n, record(n))

asked = instructor.Mode.JSON
_get_instructor_client(AnthropicClient(), "anthropic", mode=asked)
_get_instructor_client(NewGenAIClient(), "google", mode=asked)      # new google-genai SDK
_get_instructor_client(NewGenAIClient(), "openai", mode=asked)      # control: openai branch

print(f"asked mode = {asked}:")
for n, got in seen.items():
    print(f"  {'OK  ' if got == asked else 'DROP'} {n:16s} -> {got}")

Error trace

Actual output, verbatim:

asked mode = Mode.JSON:
  DROP from_anthropic   -> Mode.TOOLS
  DROP from_genai       -> Mode.TOOLS
  OK   from_openai      -> Mode.JSON

The same probe driven through the public llm_factory(...) entry point, verbatim:

llm_factory(..., mode=Mode.JSON)  ->  mode actually handed to the factory:
    DROPPED  instructor.from_genai      got Mode.TOOLS
    DROPPED  instructor.from_gemini     got Mode.MD_JSON
    DROPPED  instructor.from_anthropic  got Mode.TOOLS
    OK       instructor.from_openai     got Mode.JSON

For reference, the defaults those factories apply when no mode is passed (verbatim from the same probe):

real default modes: {'from_genai': 'Mode.TOOLS', 'from_gemini': 'Mode.MD_JSON', 'from_anthropic': 'Mode.TOOLS', 'from_perplexity': 'Mode.MD_JSON', 'from_openai': 'Mode.TOOLS'}

Expected behavior

An explicitly passed mode should reach the instructor factory for every provider branch, as it already does for openai, litellm and the generic patcher; with no mode given, the documented default Mode.JSON should apply (which the function already computes at line 585). Concrete basis:

  • src/ragas/llms/base.py:640-643 (llm_factory docstring): "mode: Instructor mode for structured outputs (default: Mode.JSON). / Only applies when using instructor adapter. / Options: Mode.JSON, Mode.MD_JSON, Mode.TOOLS, Mode.JSON_SCHEMA, etc."
  • docs/howtos/llm-adapters.md:266: "The instructor adapter supports multiple modes for structured output generation. By default, Mode.JSON is used, but you can specify a different mode for backends that don't support certain features" (and docs/howtos/llm-adapters.md:284: "Mode.JSON (default)").
  • Sibling branches of the same function forward it: from_openai (590), from_litellm (599), _patch_client_for_provider (502/517/524).
  • The existing regression test tests/unit/llms/test_instructor_factory.py::test_llm_factory_default_mode_is_json asserts this forwarding, but only for provider="openai".
  • Forwarding is valid for these providers: instructor registers Mode.JSON for both (instructor/v2/providers/genai/handlers.py, instructor/v2/providers/anthropic/handlers.py), and the installed signatures are from_genai(client, mode: Mode = Mode.TOOLS, ...) / from_anthropic(client, mode: Mode = Mode.TOOLS, ...).

This matters beyond the ignored option: _get_instructor_client's own docstring (lines 575-578) records that Mode.TOOLS mis-handles Dict type annotations in Pydantic models (returns {}), which is why ragas chose Mode.JSON as the default in the first place (see #2490). Anthropic and new-genai clients silently get exactly that Mode.TOOLS.

Additional context

Root cause: src/ragas/llms/base.py:592,595,597 (and :601) — _get_instructor_client() computes the effective mode at line 584-585 but omits the argument on the from_anthropic / from_genai / from_gemini / from_perplexity calls. These branches were simply not updated when the mode parameter was introduced in b928239 (#2565): that commit mechanically rewrote every pre-existing hardcoded instructor.Mode.JSON call to mode=mode, but these four calls had no mode argument to rewrite and were left behind.

Fix note (not a diff): a purely mechanical forward is not quite enough — instructor's v2 registry registers only a subset of modes per provider (GENAI: TOOLS + JSON; ANTHROPIC: TOOLS/PARALLEL_TOOLS/JSON/JSON_SCHEMA), so a generic mode such as Mode.MD_JSON raises ModeError for those providers. The safe shape is to keep mode at this function and forward it where the provider supports it, or normalize per provider, so the documented Mode.JSON default holds where it is valid.

Related: searched open issues and PRs for from_genai, instructor mode anthropic, llm_factory mode ignored and mode — no duplicate found. The only directly related item is #2565 (b928239), which added the parameter, and #2490, which is the reason Mode.JSON is the documented default.

Happy to open a PR with the approach above if that would help.

AI disclosure: this report was drafted with AI assistance; every claim above was verified by running the reproduction in this issue against commit 298b68274234c060deacab3cf5fb52aa3a20e885.