Skip to content

studio: return a clean model id from the OpenAI API instead of the local .gguf path - #6518

Merged
oobabooga merged 5 commits into
mainfrom
studio-model-id-sanitizer
Jun 26, 2026
Merged

oobabooga merged 5 commits into
mainfrom
studio-model-id-sanitizer

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

For a local GGUF, the OpenAI-compatible API used the absolute .gguf file path as the model id, leaking the server filesystem layout in /v1/models, /v1/models/{id}, and the model field of chat/completions responses.

Change

  • New core/inference/model_ids.py with public_model_id() (local GGUF path to file stem, HF repo id unchanged) and model_id_matches().
  • llama-server is launched with --alias <clean id> so upstream pass-through model fields are already clean.
  • Every echo site uses the clean id, including the chat/completions response model field.
  • Legacy absolute-path ids are still accepted on input for one release, but never returned.

Validation

  • test_model_ids.py and test_openai_models_path_leak.py.
  • Live: loaded a local GGUF; /v1/models and chat/completions return Qwen3-30B-A3B-Q4_K_M with no path anywhere, and the legacy path still resolves.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a mechanism to clean up model identifiers exposed via the OpenAI-compatible API, preventing local filesystem paths (such as absolute .gguf paths) from leaking to clients. It adds a new utility module model_ids.py to parse and clean model IDs, integrates it across the inference routes and llama-server command arguments, and includes comprehensive tests. The feedback suggests utilizing the newly introduced model_id_matches helper function in the model retrieval route to ensure consistent backward compatibility matching.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +6152 to +6171
from core.inference.model_ids import public_model_id

objects = _openai_model_objects()
for model in objects:
if model["id"] == model_id:
return model
# Backward compatibility: a client may still send the legacy raw identifier
# (e.g. an absolute .gguf path cached from an older /v1/models). Resolve it to
# the clean object so it keeps working, without ever echoing the path back.
llama_backend = get_llama_cpp_backend()
backend = get_inference_backend()
for raw in (
llama_backend.model_identifier if llama_backend.is_loaded else None,
backend.active_model_name or None,
):
if raw and model_id == raw:
clean = public_model_id(raw)
for model in objects:
if model["id"] == clean:
return model

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.

medium

The newly introduced model_id_matches helper function from core.inference.model_ids is specifically designed to handle backward compatibility matching between requested model IDs and internal identifiers. We should reuse it here instead of manually checking model_id == raw to ensure consistency and robustness.

Suggested change
from core.inference.model_ids import public_model_id
objects = _openai_model_objects()
for model in objects:
if model["id"] == model_id:
return model
# Backward compatibility: a client may still send the legacy raw identifier
# (e.g. an absolute .gguf path cached from an older /v1/models). Resolve it to
# the clean object so it keeps working, without ever echoing the path back.
llama_backend = get_llama_cpp_backend()
backend = get_inference_backend()
for raw in (
llama_backend.model_identifier if llama_backend.is_loaded else None,
backend.active_model_name or None,
):
if raw and model_id == raw:
clean = public_model_id(raw)
for model in objects:
if model["id"] == clean:
return model
from core.inference.model_ids import model_id_matches, public_model_id
objects = _openai_model_objects()
for model in objects:
if model["id"] == model_id:
return model
# Backward compatibility: a client may still send the legacy raw identifier
# (e.g. an absolute .gguf path cached from an older /v1/models). Resolve it to
# the clean object so it keeps working, without ever echoing the path back.
llama_backend = get_llama_cpp_backend()
backend = get_inference_backend()
for raw in (
llama_backend.model_identifier if llama_backend.is_loaded else None,
backend.active_model_name or None,
):
if raw and model_id_matches(model_id, raw):
clean = public_model_id(raw)
for model in objects:
if model["id"] == clean:
return model

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6cc877401d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return True
if len(identifier) >= 2 and identifier[1] == ":": # Windows drive, e.g. C:\
return True
if identifier.count("/") >= 2 or "\\" in identifier:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle one-level relative GGUF directories as paths

When a local GGUF is loaded via an accepted one-level relative directory such as ./model or ~/model, ModelConfig.from_identifier can keep that string as model_identifier, but _looks_like_path() returns false here because there is only one / and no .gguf suffix. Then /v1/models and the --alias added in this change expose ./model/~/model instead of a clean basename, so the path-leak fix is bypassed for local loads through those path forms. Please recognize ./~ prefixes, or derive the public id from the resolved GGUF path.

Useful? React with 👍 / 👎.

Comment on lines +5412 to +5414
_alias = public_model_id(self._model_identifier or model_path)
if _alias:
cmd.extend(["--alias", _alias])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent extra args from overriding the managed alias

This alias is supposed to keep llama-server pass-through responses aligned with GET /v1/models, but llama_extra_args are appended after Studio's flags (llama_cpp.py lines 5629-5633) and validate_extra_args still permits --alias/-a. In any load carrying that extra arg, llama.cpp's last-wins parsing can make /v1/chat/completions or /v1/completions return a different id—or the old absolute path—while /v1/models returns public_model_id(...). Treat the alias flag as Studio-managed, or strip it when emitting this clean alias.

Useful? React with 👍 / 👎.

model_info = backend.models.get(backend.active_model_name, {})
entry = {
"id": backend.active_model_name,
"id": public_model_id(backend.active_model_name),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep non-GGUF response ids consistent with models

For a local non-GGUF/LoRA load where backend.active_model_name is a filesystem path, this line now advertises only the basename in /v1/models, but the non-GGUF chat path still builds responses with model_name = backend.active_model_name (line 4537). Clients that discover the model from /v1/models will see a different model echoed by /v1/chat/completions, and the response still leaks the full local path for the same load that the listing now hides.

Useful? React with 👍 / 👎.

from core.inference.model_ids import public_model_id

# Echo a clean public id in the response, never the absolute .gguf path.
model_name = public_model_id(llama_backend.model_identifier) or payload.model

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Pass clean GGUF audio model id into response

For GGUF audio chat completions, this clean model_name is only passed to the monitor wrapper; _monitored_generate_audio() calls generate_audio(payload, request), and generate_audio recomputes model_name = llama_backend.model_identifier before serializing the JSON response. A local TTS GGUF therefore still returns the absolute .gguf path in /v1/chat/completions even though /v1/models now advertises the clean id.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen force-pushed the studio-model-id-sanitizer branch from 6cc8774 to 1e526a4 Compare June 20, 2026 14:37

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1e526a450d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +37 to +39
if identifier.count("/") >= 2 or "\\" in identifier:
return True
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Sanitize existing relative GGUF directories

Fresh case not covered by the earlier .//~ path handling: utils/paths/path_utils.py treats an existing relative path such as outputs/foo as local, and ModelConfig.from_identifier preserves that raw identifier for a detected GGUF directory. _looks_like_path() returns false for this one-slash relative path unless it has a ./ prefix or .gguf suffix, so /v1/models and --alias still expose outputs/foo instead of the clean basename. Consider using the resolved GGUF file path or checking existing relative paths before classifying org/model repo ids.

Useful? React with 👍 / 👎.

…cal .gguf path

GET /v1/models reported the loaded model's internal identifier verbatim, which
for a direct local load is the absolute .gguf path. That leaks the host
filesystem layout and is awkward for clients to round-trip as a model id.

Add core.inference.model_ids.public_model_id: a local GGUF path collapses to its
clean file stem (Qwen3-30B-A3B-Q4_K_M), while Hugging Face repo ids (org/model)
and already-clean names pass through unchanged. Use it for the id in
_openai_model_objects (both backends), have GET /v1/models/{id} still accept a
legacy raw identifier for backward compatibility while returning the clean
object, and pass --alias to llama-server so its own /v1/models and the model
field of its proxied responses are clean as well.

Sanitize every other surface that echoed the identifier back to the client, so
the path cannot leak through a different endpoint: the non-GGUF chat completion
reply, GGUF and non-GGUF audio replies, /v1/messages (Anthropic-compatible), and
all status/echo envelopes streamed from /v1/responses. A raw .gguf path sent by
a client as the request model is likewise collapsed before it is echoed.

Deny a user-supplied --alias/-a in llama-server passthrough args: Studio appends
its sanitized alias and llama.cpp's last-wins parsing would otherwise let a
caller reintroduce the path leak.

Treat relative and home paths (./model, ../model, ~/model and their Windows
forms) as local paths in public_model_id so they are sanitized rather than
returned verbatim.
@danielhanchen
danielhanchen force-pushed the studio-model-id-sanitizer branch from 1e526a4 to 18dcdf9 Compare June 22, 2026 05:27
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ae673d90f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return True
if len(identifier) >= 2 and identifier[1] == ":": # Windows drive, e.g. C:\
return True
if identifier.count("/") >= 2 or "\\" in identifier:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Sanitize one-slash local directory loads

When a local GGUF is selected via an existing relative directory such as models/Qwen, ModelConfig.from_identifier treats it as a local path and loads the GGUF inside that directory, but this predicate returns False because the identifier has exactly one slash and no .gguf suffix. The new callers then publish models/Qwen as both /v1/models id and llama-server --alias, so the OpenAI API still exposes the server-side relative path for this supported load shape. Please distinguish existing local relative paths (or derive the id from the resolved GGUF/display name) before preserving org/model repo ids.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8fc5d9e1c0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +37 to +38
if identifier.count("/") >= 2 or "\\" in identifier:
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Treat relative GGUF directories as paths

When a local GGUF is loaded from an existing relative directory such as models/foo, ModelConfig.from_identifier() keeps that original identifier while resolving the actual .gguf separately. This heuristic returns false for a one-slash directory without a .gguf suffix, so /v1/models, chat responses, and the new --alias can still expose models/foo instead of a clean basename, leaving the path leak unfixed for that supported load form.

Useful? React with 👍 / 👎.

Comment on lines +31 to +32
if identifier.lower().endswith(_GGUF_SUFFIX):
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P3 Badge Preserve repo ids whose names end in .gguf

For a remote Hugging Face GGUF repo whose id itself ends with .gguf (for example org/model.gguf), this check treats the repo id as a local file path and publishes just model, even though the helper's contract says HF repo ids should round-trip unchanged. That changes /v1/models and the llama-server --alias away from the actual loaded repo id for those valid one-slash identifiers.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9d13b6fa7c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
@oobabooga

Copy link
Copy Markdown
Member

Verified the sanitizer across path forms. Local .gguf absolute paths (and Windows, ./, ~ forms) collapse to a clean stem, while HF org/model ids round-trip untouched. Managed-flag rejection holds: --alias/-a is denied, so a user extra can't reintroduce the path. Folded in two follow-ups from this review: dropped the unused model_id_matches helper, and aligned the streamed /v1/responses model field with its non-streaming twin and chat/completions (loaded id, not the request label). Tests green.

Merging now.

@oobabooga
oobabooga merged commit 9451aef into main Jun 26, 2026
46 of 48 checks passed
@oobabooga
oobabooga deleted the studio-model-id-sanitizer branch June 26, 2026 19:07
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