Skip to content

feat(llm): reach every provider LiteLLM supports through one adapter - #99

Open
aditya-drizz wants to merge 1 commit into
mainfrom
feat/gateway-llm-adapter
Open

aditya-drizz wants to merge 1 commit into
mainfrom
feat/gateway-llm-adapter

Conversation

@aditya-drizz

Copy link
Copy Markdown
Contributor

Third of three PRs adding model selection to DrizzAI. This is the one that makes the other two live.

Why

LLMConfiguration has carried provider, model and api_key since it was written, and its docstring says "Supports any backend (Gemini, OpenAI, Anthropic) via provider field." But the factory was:

if configuration.provider == "gemini":
    return GeminiLLM(configuration=configuration)
raise NotImplementedError(...)

So the field was inert. Naming anything else raised.

One adapter, not one per vendor

LiteLLM already normalises auth, endpoints and request shape across providers. What it cannot do is bridge Fathom's own neutral types to the OpenAI-style contract it exposes — that join is ours, and it's the same join for every provider. Writing it once means Anthropic, OpenAI, Azure and Bedrock all arrive together.

Gemini keeps its native adapter. It's the tuned default and uses thinking_level / media_resolution, which have no gateway equivalent. When a gateway run is configured with them they're logged as dropped, so a quality change is attributable rather than invisible.

The four translations

prompt parts text and raw image bytes → OpenAI content blocks, media type read from magic bytes
history a model turn with tool calls → assistant message, arguments re-encoded as JSON
tools declarations are already {name, description, parameters}, so each is wrapped in the envelope
tool calls the return path — and the one that matters

That last row is where this breaks if it's going to. ToolResponseParser reads .name and .args and expects a mapping; the gateway returns arguments as a JSON string. The industry write-ups on gateway adoption flag exactly this — "a tool call that parsed end to end on the native SDK can come back in a shape your downstream parser does not expect."

It reuses FunctionCallPart rather than introducing a type, since that already has .name and .args and is Pydantic. A call whose arguments won't parse is dropped with a warning, not raised — one malformed call shouldn't discard an otherwise usable response.

Two deliberate omissions

No cost metric. The gateway prices from its own table, which drifts from the provider invoice — worst on image and cached tokens, and every step of a run carries a screenshot. Reporting a number that doesn't match the bill is worse than reporting none.

prewarm is a no-op. It exists to populate Gemini's context cache. There's no gateway equivalent, and issuing a throwaway request to imitate one would cost a call and buy nothing. Documented in the method rather than left to look like an oversight.

Keys

api_key is forwarded when set, omitted when not — in which case LiteLLM reads the ambient provider environment. That's how the Gemini path already gets its credentials, so end-to-end testing needs an env var on the crawler worker, not key storage.

Per-org encrypted keys are a separate, larger piece. The research on this is blunt: "BYOK is a sales-driven re-architecture that ripples through cost attribution, security incident response, observability, rate limiting, model-version pinning, and on-call accountability." Worth doing after the experiment says the swap works, not before.

⚠️ The real risk is not in this diff

The plumbing is testable and tested. What isn't knowable from code review is whether Fathom's prompts — written and tuned against Gemini — produce answers of the same quality on Claude or GPT. Note src/fathom/core/prompts/gemini.py already exists, so the team has met provider-specific prompting before.

Suggested next step before flipping the desktop flag: take a handful of crawler intents that pass on Gemini, run them on Claude and GPT, compare completion rate and step count. One day, and it's the only way to get that answer.

Tests

  • tests/integration/adapters/llm/test_gateway.py — 21 new. Model qualification and double-prefix, text/image content blocks, empty image rejected, system instruction ordering, history role mapping, tool envelope wrapping, tool-call name/args translation from both string and mapping, one bad call not discarding good ones, structured-output/tools conflict, usage without cost, no-choices rejection, retry succeeds and is bounded, key forwarded and omitted, lifecycle no-ops, derive.
  • Suite: 2230 passed, 219 skipped (2209 before this PR).
  • Two failures exist on main before this branch and are unaffected — test_ask_user_timeout_closes_step_and_terminates and test_adapter_passes_fathom_serde_with_allowed_modules.
  • python -m governance reports one NEW finding, which is also present on clean main (AuditColumns uses @dataclass) and is not from this change.

The factory could only build Gemini; naming any other provider raised
NotImplementedError, so LLMConfiguration's provider field was inert.

Adds a gateway adapter rather than one adapter per vendor. LiteLLM already
normalises authentication, endpoints and request shape across providers, so
the only work left is translating between Fathom's neutral types and the
OpenAI-style contract it exposes -- written once, and every provider it
supports comes with it.

Gemini keeps its native adapter. It is the tuned default and uses
provider-specific controls (thinking level, media resolution) that have no
gateway equivalent; those are logged as dropped when a gateway run is
configured with them, so a quality change is attributable rather than
invisible.

Four translations carry the weight:

  prompt parts   text and raw image bytes become OpenAI content blocks,
                 with the media type read from the payload's magic bytes
  history        a model turn carrying tool calls becomes an assistant
                 message, its arguments re-encoded as JSON
  tools          declarations are already {name, description, parameters},
                 so each is wrapped in the tool envelope
  tool calls     the return path, and the one that matters most --
                 ToolResponseParser reads .name and .args and expects a
                 mapping, while the gateway returns a JSON string

That last one reuses FunctionCallPart rather than introducing a type. A
call whose arguments will not parse is dropped with a warning instead of
raising, so one malformed call cannot discard an otherwise usable response.

Cost is deliberately absent from the reported metrics. The gateway prices
from its own table, which drifts from the provider invoice most on image
and cached tokens -- and every step of a run carries a screenshot.

litellm is an optional `gateway` extra, matching the existing per-provider
pattern, so installing Fathom for Gemini alone does not pull it in.
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.

1 participant