Skip to content

Add UCODE_MODEL_PREFERRED_PREFIX to prefer custom gateway endpoints#164

Open
tplass-ias wants to merge 1 commit into
databricks:mainfrom
tplass-ias:tplass/ucode-model-preferred-prefix
Open

Add UCODE_MODEL_PREFERRED_PREFIX to prefer custom gateway endpoints#164
tplass-ias wants to merge 1 commit into
databricks:mainfrom
tplass-ias:tplass/ucode-model-preferred-prefix

Conversation

@tplass-ias

@tplass-ias tplass-ias commented Jun 15, 2026

Copy link
Copy Markdown

Summary

Some deployments route through a custom AI Gateway prefix (e.g. companyname-claude-haiku-4-5) rather than the standard databricks-claude-* endpoints. There was no way to tell ucode configure to prefer those variants when both are available on the same workspace.

This PR adds UCODE_MODEL_PREFERRED_PREFIX — an env var that, when set, promotes any discovered model endpoint whose bare name starts with that prefix to the front of each family's candidate list. The normal newest-first ordering is preserved within the preferred and non-preferred groups.

  • Add _preferred_model_prefix() to read the env var (empty string treated as unset)
  • Add _model_bare_name() to strip any system.ai. catalog prefix before matching, so that both UC model-service IDs (system.ai.companyname-claude-haiku-4-5) and AI Gateway IDs (companyname-claude-haiku-4-5) match correctly
  • Add _sort_candidates_by_prefix(candidates, preferred_prefix, *, sort_key) — groups preferred items first using an anchored prefix check (startswith), while preserving existing within-group ordering (reverse-alpha for Claude/Codex, model_version_sort_key for Gemini)
  • Apply preference uniformly across all three model families (Claude, Gemini, Codex) and both discovery paths (UC model-services and AI Gateway v2 fallback via discover_endpoints_with_api_type)
  • Fix a pre-existing inconsistency: Codex now sorts newest-first (reverse-alpha), matching the behaviour of all other families — the old alphabetical order was an unintentional default
  • Add focused tests for preferred-prefix promotion across all four discovery paths

Usage

export UCODE_MODEL_PREFERRED_PREFIX=companyname-
ucode configure

With companyname- set, companyname-claude-haiku-4-5 will be selected over databricks-claude-haiku-4-5 when both are available, across Claude, Gemini, and Codex families.

Test plan

  • uv run ruff check . — clean
  • uv run ruff format --check . — clean
  • uv run pytest — 759 passed, 30 skipped
  • Added test_preferred_prefix_promotes_matching_model to TestDiscoverClaudeModels
  • Added test_preferred_prefix_promotes_matching_claude_model to TestDiscoverModelServices
  • Added test_preferred_prefix_promotes_matching_gemini_model to TestDiscoverGeminiModels
  • Added test_preferred_prefix_promotes_matching_codex_model to TestDiscoverGeminiModels
  • Updated test_codex_discovery_orders_newest_first to reflect corrected Codex sort order

Copilot AI review requested due to automatic review settings June 15, 2026 23:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates Databricks model discovery to support a user-configured preferred model prefix and adjusts ordering so the “best”/newest model is selected first more consistently (including Codex discovery).

Changes:

  • Add UCODE_MODEL_PREFERRED_PREFIX support to promote matching model IDs/endpoints to the top of candidate lists.
  • Centralize ordering behavior via _sort_candidates_by_prefix() and apply it across Claude/Codex/Gemini discovery paths.
  • Update Codex discovery test expectations to reflect newest-first ordering.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
tests/test_databricks.py Updates Codex discovery test to expect newest-first ordering.
src/ucode/databricks.py Adds preferred-prefix support and consolidates sorting logic across model discovery functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ucode/databricks.py Outdated
Comment thread src/ucode/databricks.py
Comment thread src/ucode/databricks.py
Comment thread src/ucode/databricks.py
@tplass-ias tplass-ias force-pushed the tplass/ucode-model-preferred-prefix branch from 3dba5cc to d2164cd Compare June 15, 2026 23:06
Set `UCODE_MODEL_PREFERRED_PREFIX` (e.g. `ias-`) to promote any
discovered model endpoint whose name contains that substring to the
front of each family's candidate list during `ucode configure`.
Preference is applied across all model families (Claude, Gemini, Codex)
and both discovery paths (UC model-services and AI Gateway v2 fallback).

- Add `_preferred_model_prefix()` to read the env var (empty string
  treated as unset).
- Add `_sort_candidates_by_prefix(candidates, preferred_prefix, *,
  sort_key)` that groups preferred items first while preserving the
  existing within-group ordering (reverse-alpha for Claude/Codex,
  `model_version_sort_key` for Gemini).
- Apply preference in `discover_model_services` for all three families.
- Thread `preferred_prefix` through `discover_endpoints_with_api_type`
  so `discover_gemini_models` and `discover_codex_models` (AI Gateway
  fallback) also respect the setting.
- Update the one test that asserted alphabetical Codex order, which was
  the unintentional pre-existing default; Codex now sorts newest-first
  consistently with the other families.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 15, 2026 23:13
@tplass-ias tplass-ias force-pushed the tplass/ucode-model-preferred-prefix branch from d2164cd to 44abf96 Compare June 15, 2026 23:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread src/ucode/databricks.py
Comment on lines +1327 to +1337
# Accept both the standard databricks-claude-* prefix and any custom
# preferred prefix so that e.g. companyname-claude-haiku-4-5 is discoverable.
family_ids = [
m
for m in raw_ids
if f"claude-{family}-" in m
and (
f"databricks-claude-{family}-" in m
or (preferred_prefix and _model_bare_name(m).startswith(preferred_prefix))
)
]
Comment thread src/ucode/databricks.py
Comment on lines 1396 to +1398
describes why the list is empty. `sort_key` overrides the default
alphabetical ordering of the returned names.
newest-first (reverse-alphabetical) ordering of the returned names. `preferred_prefix` promotes
matching endpoints to the front of the list (see ``_preferred_model_prefix``).
Comment thread src/ucode/databricks.py
out.append(name)
if out:
return sorted(out, key=sort_key), None
return _sort_candidates_by_prefix(out, preferred_prefix, sort_key=sort_key), None
Comment thread src/ucode/databricks.py
Comment on lines +89 to +97
preferred = [m for m in candidates if _model_bare_name(m).startswith(preferred_prefix)]
others = [m for m in candidates if not _model_bare_name(m).startswith(preferred_prefix)]
if sort_key:
return sorted(preferred, key=sort_key) + sorted(others, key=sort_key)
return sorted(
candidates,
key=lambda m: (_model_bare_name(m).startswith(preferred_prefix), m),
reverse=True,
)
Comment thread src/ucode/databricks.py
Comment on lines 1396 to +1398
describes why the list is empty. `sort_key` overrides the default
alphabetical ordering of the returned names.
newest-first (reverse-alphabetical) ordering of the returned names. `preferred_prefix` promotes
matching endpoints to the front of the list (see ``_preferred_model_prefix``).
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.

2 participants