feat: add context_length to /v1/models response#933
Open
lichengzhe wants to merge 1 commit intoml-explore:mainfrom
Open
feat: add context_length to /v1/models response#933lichengzhe wants to merge 1 commit intoml-explore:mainfrom
lichengzhe wants to merge 1 commit intoml-explore:mainfrom
Conversation
Read max_position_embeddings from each model's config.json and include it as context_length in the /v1/models endpoint response. This helps API gateways (e.g. one-api, new-api) and clients automatically discover the model's context window size without manual configuration. Handles both HuggingFace cached models and local path models, including models that nest the value under text_config.
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.
Summary
Include
context_lengthin the/v1/modelsendpoint response by readingmax_position_embeddingsfrom each model'sconfig.json.Motivation
Many OpenAI-compatible API gateways and proxy tools (e.g. one-api, new-api) read
context_lengthfrom the/v1/modelsresponse to automatically determine a model's context window size. Without this field, users must manually configure context limits for each model, which is error-prone and tedious.Related: #966 (KeyError on context_length)
Changes
get_context_length()helper that readsmax_position_embeddingsfrom a HuggingFace cached model'sconfig.jsoncontext_lengthwhen availablecontext_lengthtext_config(e.g. some vision-language models)config.jsonis missing or unreadableExample response
{ "object": "list", "data": [ { "id": "RepublicOfKorokke/Qwen3.5-35B-A3B-mlx-lm-mxfp4", "object": "model", "created": 1740000000, "context_length": 262144 } ] }Testing
Tested locally with
Qwen3.5-35B-A3B-mlx-lm-mxfp4— confirmedcontext_length: 262144appears in the response.