Skip to content

feat(api): implement GET /v1/models/{model} retrieve model endpoint - #46

Merged
Mag1cFall merged 1 commit into
Mag1cFall:mainfrom
IllusionOfControl:feature/openai-get-model
Oct 4, 2026
Merged

Mag1cFall merged 1 commit into
Mag1cFall:mainfrom
IllusionOfControl:feature/openai-get-model

Conversation

@IllusionOfControl

Copy link
Copy Markdown
Contributor

Description

Summary

Implements the GET /v1/models/{model} endpoint for retrieving individual model metadata, adhering to the OpenAI Models API specification (client.models.retrieve(...)).

Previously, only GET /v1/models (listing all models) and GET /v1beta/models/{model} (Gemini single-model route) were available. Standard OpenAI SDKs and third-party tools querying GET /v1/models/{model} received a 404 Not Found response.

Key Changes

  1. Routing (internal/api/router.go):

    • Registered GET /v1/models/{model...} in the public http.ServeMux.
    • Used {model...} wildcard pattern to accommodate both plain model IDs (gemini-2.5-flash) and prefixed/namespaced identifiers (models/gemini-2.5-flash).
  2. OpenAI Model Handler (internal/api/openai.go):

    • Extracted openAIModelObject(model aistudio.Model) map[string]any helper to ensure shared schema consistency between model list and retrieve endpoints.
    • Implemented handleOpenAIModel(w http.ResponseWriter, r *http.Request):
      • Resolves models by canonical ID, models/ prefix, and configured aliases (capability_options.aliases).
      • Returns standard OpenAI model object (object: "model", limits, methods, capabilities, paid flags).
      • Returns 404 Not Found with standardized OpenAI error structure (code: "model_not_found", type: "invalid_request_error") if the requested model does not exist.
  3. Anthropic Compatibility (internal/api/anthropic.go):

    • Added writeAnthropicModel(w http.ResponseWriter, model aistudio.Model).
    • When requests include the Anthropic-Version header, returns Anthropic-formatted model objects (type: "model"), or not_found_error on 404.
  4. Unit Tests (internal/api/models_test.go):

    • Added test suite TestOpenAIModelsEndpoint covering:
      • list models openai: verifying GET /v1/models format.
      • list models anthropic: verifying GET /v1/models with Anthropic-Version.
      • retrieve model by id: verifying GET /v1/models/gemini-2.5-flash.
      • retrieve model with prefix models/: verifying GET /v1/models/models/gemini-2.5-flash.
      • retrieve model by alias: verifying lookup via alias configured in capability_options.aliases.
      • retrieve model not found: validating 404 status and model_not_found error payload.
      • retrieve model anthropic found: validating Anthropic response structure.
      • retrieve model anthropic not found: validating 404 Anthropic not_found_error.
  5. Documentation:

    • Updated endpoint reference tables in README.md, README_en.md, docs/protocol.md, and docs/development.md.

Verification

  • go test -v ./internal/api -run TestOpenAIModelsEndpoint: all subtests passed.
  • go test ./...: all package tests across repository passed.
  • go vet ./...: 0 warnings or errors.

- Register GET /v1/models/{model...} route to support retrieving single model details
- Support model lookup by canonical ID, 'models/' prefix, and configured aliases
- Return Anthropic model object when Anthropic-Version header is present
- Return standard 404 model_not_found error when model does not exist
- Add comprehensive unit tests in internal/api/models_test.go
- Update API endpoint documentation in README and docs
@Mag1cFall
Mag1cFall merged commit 8d26090 into Mag1cFall:main Oct 4, 2026
2 checks passed
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