Skip to content

feat(serve): GET /v1/models reports certified thresholds and context - #64

Merged
rkinas merged 1 commit into
rkinas:mainfrom
pawelkiszczak:pr/models-certified-context
Nov 2, 2026
Merged

rkinas merged 1 commit into
rkinas:mainfrom
pawelkiszczak:pr/models-certified-context

Conversation

@pawelkiszczak

@pawelkiszczak pawelkiszczak commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #63

What

Adds certified_table(cal) using the existing certified helper. /v1/models reports certified thresholds for matching calibrations and the server's context limit, overflow policy, and truncation head share.

Why

Clients can make informed automation and prompt-sizing decisions from the serving metadata alone.

Compatibility

Existing models_payload callers remain compatible; the new optional fields are absent unless supplied. The endpoint adds certified and context fields. All existing model metadata remains unchanged. A mismatched calibration omits certified; a server with no thresholds reports an empty table.

Tests run

  • uv run --no-sync pytest -q tests/test_server.py tests/test_http_status.py: 79 passed.
  • uv run --no-sync pytest -q -x: 763 passed, 53 skipped, 1 warning.
  • uvx [email protected] check --select E9,F63,F7,F82 --target-version py310 .: failed on existing basal/gemma4.py:471 undefined name kpos, outside this change.

Summary by Sourcery

Publish calibration confidence thresholds and context limits through the model-serving metadata endpoint.

New Features:

  • Expose certified confidence thresholds by error target and question type in GET /v1/models when calibration matches the served model.
  • Expose serving context metadata, including the context limit, overflow policy, and truncation head share, in GET /v1/models.

Enhancements:

  • Preserve compatibility by making the new model metadata fields optional and omitting certification for mismatched calibrations.

Documentation:

  • Document the new certified and context fields returned by GET /v1/models and add them to the unreleased changes.

Tests:

  • Add coverage for certified threshold formatting, optional model metadata, context reporting, and mismatched calibration handling.

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR extends GET /v1/models with optional certified calibration thresholds and context-management metadata, using the existing certification logic and suppressing certification for mismatched calibrations. It preserves models_payload compatibility, documents the response contract, and adds focused unit and HTTP coverage.

Sequence diagram for GET /v1/models metadata enrichment

sequenceDiagram
    participant Client
    participant Server
    participant Calibration

    Client->>Server: GET /v1/models
    Server->>Calibration: certified_table(cal)
    Calibration-->>Server: certified thresholds by target and question type
    Server-->>Client: models_payload(..., certified, context)
    Note over Server,Client: Response includes context limit, overflow, and truncate_head
Loading

Flow diagram for certification metadata eligibility

flowchart TD
    A[GET /v1/models] --> B{Calibration matches served model?}
    B -->|No| C[Omit certified]
    B -->|Yes| D["certified_table(cal)"]
    D --> E[Report thresholds by max_error and question type]
    C --> F[Report context metadata]
    E --> F
    F --> G[Return model metadata]
Loading

File-Level Changes

Change Details Files
Expose certified calibration thresholds and serving context through the models metadata endpoint.
  • Build a compact threshold-by-question-type table using the existing certification lookup, omitting unavailable values and empty rows.
  • Add optional certified and context fields to the model payload without changing existing callers or metadata.
  • Populate certification only for matching calibrations and report context limit, overflow policy, and truncation head share.
  • Document the new endpoint fields and unreleased feature.
basal/server.py
docs/API.md
docs/WHATS_NEW.md
Add unit and endpoint coverage for metadata compatibility and conditional certification reporting.
  • Verify threshold filtering, empty calibrations, optional payload fields, and context serialization.
  • Verify the endpoint reports context and hides certified thresholds when calibration metadata is mismatched.
tests/test_server.py
tests/test_http_status.py

Assessment against linked issues

Issue Objective Addressed Explanation
#63 Expose certified confidence thresholds in GET /v1/models by max_error target and question type, including only choice, noul, and score, omitting unavailable thresholds and withholding certification data for mismatched calibrations. ✅
#63 Expose the server’s effective context limit, overflow policy, and truncation head share in GET /v1/models. ✅
#63 Preserve all pre-existing model metadata and behavior while adding the new response fields. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="basal/server.py" line_range="321" />
<code_context>
+    what a client needs to tell automatable decisions from those to review."""
+    table = {}
+    for target in (cal.get("thresholds") or {}):
+        row = {t: c for t in ("choice", "noul", "score") if (c := certified(cal, float(target), t)) is not None}
+        if row:
+            table[target] = row
</code_context>
<issue_to_address>
**Model metadata requests fail**

When a loaded calibration contains a nonnumeric key under `thresholds`, `check_calibration` does not validate threshold keys, so `certified_table` calls `float(target)` and raises `ValueError`; `GET /v1/models` returns 500 instead of model metadata.

Validate threshold keys when loading the calibration or handle nonnumeric keys in `certified_table` without raising.
</issue_to_address>

### Comment 2
<location path="basal/server.py" line_range="1657" />
<code_context>
-                                           getattr(srv, "layout", None)))
+                                           getattr(srv, "layout", None),
+                                           certified=None if getattr(srv, "cal_mismatch", None) else
+                                           certified_table(srv.cal),
+                                           context={"limit": srv.context[0] if srv.context else None,
+                                                    "overflow": srv.overflow, "truncate_head": srv.trunc_head}))
</code_context>
<issue_to_address>
**Invalid calibration appears empty**

When the calibration has mismatched layout or option orders but no model or revision mismatch, `load_calibration` strips the thresholds and sets `cal_unvalidated`, but `models()` checks only `cal_mismatch` before calling `certified_table`; the response includes `"certified": {}` instead of omitting the field, so clients cannot distinguish an invalid calibration from a matching calibration with no thresholds.

Have `models()` omit `certified` when the calibration is marked `cal_unvalidated`, as well as when it has a `cal_mismatch`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/server.py
what a client needs to tell automatable decisions from those to review."""
table = {}
for target in (cal.get("thresholds") or {}):
row = {t: c for t in ("choice", "noul", "score") if (c := certified(cal, float(target), t)) is not None}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Model metadata requests fail

When a loaded calibration contains a nonnumeric key under thresholds, check_calibration does not validate threshold keys, so certified_table calls float(target) and raises ValueError; GET /v1/models returns 500 instead of model metadata.

Validate threshold keys when loading the calibration or handle nonnumeric keys in certified_table without raising.

Prompt for AI agents
In `basal/server.py` at line 321:

**Model metadata requests fail**

When a loaded calibration contains a nonnumeric key under `thresholds`, `check_calibration` does not validate threshold keys, so `certified_table` calls `float(target)` and raises `ValueError`; `GET /v1/models` returns 500 instead of model metadata.

Validate threshold keys when loading the calibration or handle nonnumeric keys in `certified_table` without raising.

Comment thread basal/server.py
getattr(srv, "layout", None)))
getattr(srv, "layout", None),
certified=None if getattr(srv, "cal_mismatch", None) else
certified_table(srv.cal),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Invalid calibration appears empty

When the calibration has mismatched layout or option orders but no model or revision mismatch, load_calibration strips the thresholds and sets cal_unvalidated, but models() checks only cal_mismatch before calling certified_table; the response includes "certified": {} instead of omitting the field, so clients cannot distinguish an invalid calibration from a matching calibration with no thresholds.

Have models() omit certified when the calibration is marked cal_unvalidated, as well as when it has a cal_mismatch.

Prompt for AI agents
In `basal/server.py` at line 1657:

**Invalid calibration appears empty**

When the calibration has mismatched layout or option orders but no model or revision mismatch, `load_calibration` strips the thresholds and sets `cal_unvalidated`, but `models()` checks only `cal_mismatch` before calling `certified_table`; the response includes `"certified": {}` instead of omitting the field, so clients cannot distinguish an invalid calibration from a matching calibration with no thresholds.

Have `models()` omit `certified` when the calibration is marked `cal_unvalidated`, as well as when it has a `cal_mismatch`.

@rkinas

rkinas commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thank you, Paweł! Planned for basal 2.5: we are merging this into the 2.5 release branch now, after our checks (full CPU suite; /v1/models unchanged apart from the new certified and context fields). The PR will close when 2.5 is published on main.

@rkinas
rkinas merged commit ed8f0e2 into rkinas:main Nov 2, 2026
1 check passed
rkinas added a commit that referenced this pull request Nov 2, 2026
… version

CONTRIBUTING.md credits the 2.5 pull requests as the release notes' community line lists them (Paweł Kiszczak
#52, #62, #64, #66, #68, #71, #73, #75; Marcin Brzeżański #56; Grzegorz Brzezinka #76, #78) and adds Dariusz
Koryto (#53, #58, #59, #60 and #55); the README's list names him too. docs/changes/README.md and the
changelog.py help use 2.5.1, the next release, as the example.
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.

Expose calibration certificates and context in GET /v1/models

2 participants