Skip to content

feat(adapters)!: prompt fallback for adapter functions without a trained adapter - #1654

Merged
ajbozarth merged 7 commits into
generative-computing:mainfrom
ajbozarth:issue-993-granite-library-fallbacks
Nov 5, 2026
Merged

ajbozarth merged 7 commits into
generative-computing:mainfrom
ajbozarth:issue-993-granite-library-fallbacks

Conversation

@ajbozarth

@ajbozarth ajbozarth commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Pull Request

Issue

Fixes #993
(Indirectly) Fixes #1627

Description

Adds prompt-based fallbacks for Granite adapter functions on the LocalHF backend. When no trained aLoRA or LoRA adapter exists for a capability on the target model, resolution now falls back to a hand-authored prompt (packaged as an io.yaml instruction) run against the plain base model instead of failing — the only mechanism available for models a LoRA cannot be loaded into.

resolve_adapter walks the chain per capability + base model and registers the first rung that exists: model-specific aLoRA → model-specific LoRA → model-specific prompt → model-agnostic any prompt (with a warning on the any rung, since it is less accurate than a model-tuned one).

What's included:

  • PromptBinding — a weightless WeightsBinding (no-op prepare/deactivate/release; activate() only logs), plus AdapterType.PROMPT and a widened Identity.adapter_type.
  • Availability-aware resolve_adapter (_register_available_composed_adapter) that probes the chain via a tiny io.yaml fetch, replacing the previous hardcoded LoRA registration.
  • A weightless HF generation path (_generate_composed_prompt) that runs the intrinsic's injected instruction against the base model with no adapter_scope.
  • Generalized adapter-function invocation telemetry (_afire_invocation_complete / _await_generation_reporting_error) that now carries binding_type and revision; the OpenAI embedded path is migrated onto the shared helpers.
  • Path-helper support for the prompt subdir and the any model slot (adapter_subpath / obtain_io_yaml now take an adapter_type string; new resolve_prompt_io_yaml).
  • AdapterNotFoundError (a ValueError subclass) for the not-found signal obtain_lora raises, so the resolution walk and resolve_prompt_io_yaml advance only on a genuine miss and let other ValueErrors (e.g. a Hugging Face HFValidationError) propagate instead of being masked as "not found".
  • Documentation for the prompt fallback (docs/docs/advanced/intrinsics.md) and the binding-shape references.

Behavior change worth a close look: because resolution is now availability-aware, it selects an aLoRA adapter where one exists on the LocalFile path — previously that path hardcoded LoRA, so an aLoRA was never auto-selected there. This closes a gap documented against Epic #929 "Phase 2", which has no discrete tracked issue of its own. The aLoRA-via-LocalFile path was not previously exercised in production, so it is worth confirming it loads and generates as expected.

Breaking changes

Three path helpers in mellea.formatters.granite.intrinsics change signature. All predate this PR; the ! covers them.

  • obtain_lora, obtain_io_yaml, and adapter_subpath drop the alora: bool parameter in favor of adapter_type: str ("lora" | "alora" | "prompt"). A caller passing alora=True switches to adapter_type="alora"; a positional caller must account for the removed parameter.
  • On obtain_lora and obtain_io_yaml, revision, cache_dir, and file_glob are now keyword-only.

obtain_lora and obtain_io_yaml are exported in __all__; adapter_subpath is module-public but not exported. In-repo callers all use keywords, so only out-of-tree callers are affected.

Deferred to a follow-up PR: OpenAI and Ollama prompt fallback. Ollama landed as a third adapter backend on main while this work was in progress, so the follow-up will design the served-adapter prompt fallback uniformly across OpenAI and Ollama in the resolution layer (registering a PromptBinding so the high-level intrinsic helpers reach it) rather than per-backend. This PR is LocalHF-only and self-contained; the weights-binding support table reflects that (LocalHF ✅, OpenAI / Ollama —).

Note on the e2e tests: the end-to-end resolution tests target a temporary private repo (ajbozarth/granitelib-rag-r1.0, a weights-stripped copy of ibm-granite/granitelib-rag-r1.0 with prompt io.yaml files added). Because that repo is private, the tests xfail (rather than hard-fail) wherever it is not readable with the current token — including CI's public-only token and any other developer's token — and run normally for a developer with access. They should be retargeted to a public repo once one carries this layout (marked with a TODO in the test), at which point the access guard goes inert.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

New unit tests (no network): prompt-subpath / path-helper resolution, PromptBinding verbs + AdapterType.PROMPT, the availability walk across every rung (trained hit, LoRA fallthrough, model-specific prompt, any prompt + warning, hard-error abort, terminal not-found), and the weightless HF generate path. New e2e tests exercise the resolution chain — including an aLoRA-preference and a LoRA-only case — against the test repo. Ran the adapter / HF / OpenAI / Ollama / formatter unit + integration subsets locally after rebasing on main; the full suite runs in CI.

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

…ned adapter

When no aLoRA or LoRA adapter exists for a capability on the target model,
resolution falls back to a hand-authored prompt (an io.yaml instruction) run
against the plain base model instead of failing. resolve_adapter probes
aLoRA -> LoRA -> model-specific prompt -> model-agnostic "any" prompt (warning
on "any") and registers the first that exists. Implemented for LocalHFBackend.

- PromptBinding: weightless WeightsBinding (no-op verbs; activate() logs) plus
  AdapterType.PROMPT; Identity accepts "prompt".
- resolve_adapter is availability-aware, replacing the hardcoded LoRA
  registration; it now selects aLoRA where one exists on the LocalFile path,
  which the hardcoded path never reached.
- Weightless HF generate path runs against the base model with no adapter_scope.
- util: adapter_subpath/obtain_io_yaml take adapter_type (str, was alora: bool);
  new resolve_prompt_io_yaml.
- Add AdapterNotFoundError (a ValueError subclass) for the not-found signal, so
  the fallback walk advances only on a genuine miss and other ValueErrors
  (e.g. HFValidationError) propagate rather than being masked.
- Generalize the invocation-complete telemetry helpers to carry binding_type
  and revision (was embedded-only); OpenAI's embedded path migrated onto them.
- Tests and docs for the prompt fallback.

BREAKING CHANGE: obtain_lora and obtain_io_yaml (public, exported from
mellea.formatters.granite.intrinsics) replace the `alora: bool` parameter with
`adapter_type: str` ("lora" | "alora" | "prompt"). Keyword callers passing
alora=True must switch to adapter_type="alora"; positional callers must account
for the removed parameter.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth
ajbozarth requested a review from a team as a code owner September 23, 2026 17:26
@ajbozarth ajbozarth added area/backends Provider-specific work: Ollama, HF, LiteLLM, OpenAI, Bedrock, vLLM area/adapter-functions Granite adapter functions: framework and adaptiers including RAG, Guardian, Core labels Sep 23, 2026
@ajbozarth
ajbozarth requested a review from markstur September 23, 2026 17:26
@ajbozarth ajbozarth added the area/formatter Output formatters, structured output, GraniteFormatter, JSON schema formatting label Sep 23, 2026
@ajbozarth ajbozarth self-assigned this Sep 23, 2026
@ajbozarth ajbozarth added area/backends Provider-specific work: Ollama, HF, LiteLLM, OpenAI, Bedrock, vLLM area/adapter-functions Granite adapter functions: framework and adaptiers including RAG, Guardian, Core area/formatter Output formatters, structured output, GraniteFormatter, JSON schema formatting labels Sep 23, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Sep 23, 2026

@planetf1 planetf1 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 4643f67. Thanks for this, and particularly for the "behaviour change worth a close look" paragraph. It sent me to measure the aLoRA selection change on a real model, and that turned out to be the most consequential thing here.

The design holds up. All five decisions from #993's 2026-09-03 meeting are implemented as agreed, including the subtle one: AdapterNotFoundError makes the fallback a genuine not-found mechanism, so a Hub auth or validation error still propagates. The walk is 92% covered with every rung exercised, and PromptBinding is at 100%.

Four asks, each on the line it concerns:

  1. huggingface.py:1244 an explicit adapter_types no longer has any effect on this backend.
  2. adapter.py:539 preferring aLoRA regresses requirement checking today. The ask is small: keep today's rung order and link the issues. The gate belongs in #1678.
  3. adapter.py:588 a prompt io.yaml with no instruction returns a fabricated score.
  4. huggingface.py:3017 and :1408 the branches that make the feature work are not executed by any test. They do work, I ran them, so this is test debt.

Issues opened from this review: #1678 aLoRA activation is unverifiable, #1679 the published requirement-check aLoRA, #1680 result-level provenance, #1681 a coverage rule that hides these generation helpers from measurement.

Documentation points that have no line to sit on
  • The title carries !, but the body never lists what breaks. As far as I can tell: the three path-helper signatures losing alora, the Identity.adapter_type widening for exhaustive consumers, and whatever validation-mechanism change survives ask 2.
  • Sibling docs the new binding did not reach: metrics.py:1236, docs/docs/observability/metrics.md:317, the module list at _core.py:10-18, the stale Literal["lora", "alora"] at adapter.py:314, and the config= contract at adapter.py:674-681 and huggingface.py:2889-2896, which still says config is rejected for every shape other than Embedded while the code now accepts it for Prompt. The "composed Embedded adapter" wording in list_adapters and remove_adapter covers Prompt too now.
  • Two sentences would help the next reader: why probing the pinned revision beats the catalogue for availability, given the epic's removed TODO said selection would come from the catalogue; and that prompt fallback is LocalHF-only today.
  • The e2e test's TODO could cite #1655, so the stopgap is traceable from the code.
Things I checked that came back clean

Registry symmetry, so a prompt adapter can be listed and removed like the others. Lock ordering in the new registration branch, which keeps resolve then activation and does its fetch outside the activation lock. Enum widening, where every AdapterType consumer either asserts the two trained types or reads the catalogue. The Ollama signature port. Exports and __all__ in both packages. And the obvious worry from #1634, that RAG documents might be dropped on a new path: they cannot be, because the weightless path reuses the same rewriter output and the same generate_input.

Also #1655 already covers the private e2e fixture repository with acceptance criteria, which is more than most stopgaps get.

Comment thread mellea/backends/huggingface.py Resolved
Comment thread mellea/backends/adapters/adapter.py Resolved
Comment thread mellea/backends/adapters/adapter.py Resolved
Comment thread mellea/backends/huggingface.py Resolved
Comment thread mellea/backends/huggingface.py Resolved
Comment thread mellea/backends/adapters/_core.py Resolved
Comment thread mellea/backends/adapters/_core.py Resolved
Comment thread docs/docs/advanced/intrinsics.md Resolved
Comment thread mellea/backends/adapters/adapter.py Resolved

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up to my earlier review, from re-running the measurement behind "preferring aLoRA regresses requirement checking today". The premise has changed; see the updated #1679 and #1685.

  • The aLoRA was never activated on this backend at all: adapters load through the bare model.load_adapter() path, so PEFT's aLoRA offset computation (which only runs inside the PeftModel wrapper) never executed. Zero hook calls, measured. That bug is fixed in #1685.
  • The weights are not broken: the uncertainty aLoRA works as-published once activation is fixed, and the requirement-check items previously attributed to the weights answer correctly under activation.
  • The requirement-check io.yaml tokenization mismatch is real; the durable fix is a granitelib republish, and #1685 carries a removable repair in the meantime.

What this means: aLoRA is broken on main today, and that is exactly why this PR's switch to preferring aLoRA was a problem when I reviewed it. If #1685 merges first, that switch becomes an improvement instead, and I'll take back that part of my review. #1678 (aLoRA silently never activating) is fixed by #1685, so it can close with it. My other points in the review are unchanged.

@jakelorocco jakelorocco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Nigel's comments. I deleted my duplicate comments that raised the same issues.

Comment thread mellea/backends/adapters/_core.py Outdated
Comment on lines +1011 to +1012
binding_type: str,
revision: str | None = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not just pass the WeightsBinding object here instead of its sub-pieces? Similar to how the actual Identity object is getting passed in as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea, updated in f740378

… check

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

Leaving a note here as this has sat seemly untouched for a while: I am actively working through the above review and hope to push an update, respond to comments, and open follow up issue soon (maybe tomorrow hopefully no later than Monday)

…or prompt fallback

Harden prompt-fallback resolution: validate the io.yaml at resolve
time (via make_config_dict, requiring a non-empty instruction) and
honor the catalogue's adapter_types in the walk. Make PromptBinding's
type structural, the path helpers' kwargs keyword-only, and pass the
weights binding to telemetry rather than unpacked fields. Soften the
terminal miss message so it doesn't claim a repo is missing an adapter
when the cache is just offline.

Add seam/narrowing unit coverage and document the fallback's quality,
provenance, and implicit-registration routing in the adapter docs.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

@planetf1 @markstur @jakelorocco thank you for the review, I have finally finished rebasing on latest main and addressing all your feedback.

Everything should be covered in f740378 or by one of the three follow up issues I have opened to continue this work after this PR merges:

If you could please re-review to make sure we've caught everything. Also worth noting now that #1748 is open for followup this PR is "finalized" for review

@ajbozarth

Copy link
Copy Markdown
Member Author

@planetf1 @markstur @jakelorocco soft ping for re-review

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good, apart from the inline comments.

Comment thread mellea/backends/adapters/adapter.py Resolved
Comment thread docs/docs/advanced/intrinsics.md Resolved
Comment thread docs/docs/advanced/intrinsics.md Resolved
Comment thread mellea/backends/adapters/adapter.py Resolved
Comment thread mellea/backends/adapters/_core.py Resolved
Comment thread mellea/backends/adapters/adapter.py Resolved
make_config_dict's return type is now `dict`, not `dict | None`: it always
raised rather than returning None, so callers no longer need a None guard.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

@planetf1 I should have address every new/open review thread. In addition I have expanded the followup issue #1655 to target all updates required once prompt support exists on HF

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajbozarth All looks good here. thanks for that last batch of updates. I was also able to check running end to end

@ajbozarth
ajbozarth added this pull request to the merge queue Nov 5, 2026
Merged via the queue into generative-computing:main with commit a3d8e82 Nov 5, 2026
12 checks passed
@ajbozarth
ajbozarth deleted the issue-993-granite-library-fallbacks branch November 5, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/adapter-functions Granite adapter functions: framework and adaptiers including RAG, Guardian, Core area/backends Provider-specific work: Ollama, HF, LiteLLM, OpenAI, Bedrock, vLLM area/formatter Output formatters, structured output, GraniteFormatter, JSON schema formatting enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: reorder adapter catalog download order Prompting Fallbacks for Granite Library Intrinsics

4 participants