Include context_length in /v1/models response (#1183)#1184
Open
seikixtc wants to merge 1 commit intoml-explore:mainfrom
Open
Include context_length in /v1/models response (#1183)#1184seikixtc wants to merge 1 commit intoml-explore:mainfrom
seikixtc wants to merge 1 commit intoml-explore:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1183.
What
Adds a
context_lengthfield to every entry returned by the/v1/modelsendpoint (and the/v1/models/{repo_id}single-model variant), reporting the maximum context length the model declares in itsconfig.json.Example response after this change:
{ "object": "list", "data": [ { "id": "mlx-community/Qwen2.5-7B-Instruct-4bit", "object": "model", "created": 1745000000, "context_length": 32768 } ] }Why
OpenAI-compatible clients increasingly need to know a model's usable context window before constructing a request — for chunking long documents, sizing prompt caches, choosing between models, and rendering token-budget UIs. Today the endpoint exposes only
id,object, andcreated, so clients have to hard-code limits or guess.How
_get_context_length(config_path)to read the first recognized max-context field fromconfig.jsonmax_position_embeddings,n_positions,max_sequence_length, andseq_length_find_repo_config_path(repo)so Hugging Face cache scans use the top-level snapshotconfig.jsonrather than any nested file with the same basenameNoneinstead of raising if the config is missing, malformed, or has no valid positive integer context fieldcontext_lengthin both cached-model listings and local--modellistingsTests
_get_context_lengthcover recognized fields, priority ordering, missing fields, malformed JSON, missing files, non-positive values, string values, bool values, andNoneinput_find_repo_config_pathcover top-level-vs-nestedconfig.jsonselection in Hugging Face cache snapshotstest_handle_modelsnow asserts every model entry includescontext_length, with value eitherNoneor a positive integerVerification
python3 -m unittest discover -s tests -p 'test_server.py' -vpre-commit run --files mlx_lm/server.py tests/test_server.pygit diff --checkBackward compatibility
Purely additive. Existing clients that only read
id/object/createdcontinue to work unchanged.