Skip to main content

Fine-tuning

The gateway proxies the OpenAI fine-tuning API, so teams can fine-tune models on OpenAI and Azure OpenAI with their virtual keys instead of provider credentials, and start jobs on Vertex AI. Training files are uploaded through the files API, jobs are created against a provider, and the resulting models can be registered in model_list like any other model. On Vertex AI the gateway can only create a job: retrieve, list, cancel, events, checkpoints, pause and resume work for OpenAI and Azure OpenAI only, so follow a Vertex AI job in the Google Cloud console. Fine-tuning endpoints are part of Spanway Enterprise and need any valid license. Without one, every fine-tuning route returns 403 with the message Fine-tuning needs a Spanway enterprise license and the request never reaches the provider.

Scenario​

A support team wants a small model that classifies tickets in their own taxonomy. They upload a training file of labelled tickets, create a fine-tuning job on their OpenAI account through the gateway, follow its events until it finishes, and register the fine-tuned model under a public name so their application can call it with the same key. Nobody on the team handles the provider key.

Steps​

  1. Configure the provider credentials for jobs and training files in config.yaml, then restart the gateway. Every provider you name in a call needs its own finetune_settings entry. A call that names a provider with no entry is refused with 400, so the gateway never falls back to whatever provider key its own process happens to hold.

    finetune_settings:
    - custom_llm_provider: openai
    api_key: os.environ/OPENAI_API_KEY
    - custom_llm_provider: azure
    api_base: os.environ/AZURE_API_BASE
    api_key: os.environ/AZURE_API_KEY
    api_version: "2024-05-01-preview"
    - custom_llm_provider: vertex_ai
    vertex_project: os.environ/VERTEXAI_PROJECT
    vertex_location: us-central1

    files_settings:
    - custom_llm_provider: openai
    api_key: os.environ/OPENAI_API_KEY
  2. Create a key in Virtual Keys (/ui/api-keys) with + Create New Key.

  3. Upload the training file and create the job.

    curl -X POST http://localhost:4000/v1/files \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -F "purpose=fine-tune" \
    -F "file=@train.jsonl"

    curl -X POST http://localhost:4000/v1/fine_tuning/jobs \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"model": "gpt-4.1-mini-2025-04-14", "training_file": "file-abc123", "custom_llm_provider": "openai", "hyperparameters": {"n_epochs": 3}}'
  4. Follow the job. GET /v1/fine_tuning/jobs/{id}?custom_llm_provider=openai shows the status and the fine_tuned_model name once finished, GET .../events lists the training log, and POST .../cancel, /pause and /resume control it. To list the jobs of one model's credentials instead of a provider account, pass exactly one model name.

    curl "http://localhost:4000/v1/fine_tuning/jobs?target_model_names=gpt-4.1-mini&limit=5" \
    -H "Authorization: Bearer $SPANWAY_API_KEY"
  5. Register the fine-tuned model. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints), click Add Model, pick the provider, choose Custom Model Name and type the fine-tuned model name, set the Public Model Name and click Add Model. In config.yaml set base_model in model_info so cost tracking uses the right price tier.

    model_list:
    - model_name: support-classifier
    spanway_params:
    model: openai/ft:gpt-4.1-mini-2025-04-14:examplecorp::abc123
    api_key: os.environ/OPENAI_API_KEY
    model_info:
    base_model: gpt-4.1-mini-2025-04-14

Expected result​

{"id": "ftjob-abc123", "object": "fine_tuning.job", "model": "gpt-4.1-mini-2025-04-14", "created_at": 1760441234, "status": "queued", "training_file": "file-abc123", "hyperparameters": {"n_epochs": 3}}

The job moves through validating_files, queued, running and succeeded, and the finished job carries fine_tuned_model. Create, retrieve, cancel, events, checkpoints, pause and resume appear in Logs (/ui/logs) under the key. Listing jobs is not logged.

The gateway does not price the training run itself. The provider bills it to its own account, outside gateway spend. Once the fine-tuned model is registered, its inference calls are priced like any other model through base_model or custom pricing.

Endpoints​

MethodPathSummary
GET/v1/fine_tuning/jobsList fine-tuning jobs
POST/v1/fine_tuning/jobsCreate a fine-tuning job
GET/v1/fine_tuning/jobs/{fine_tuning_job_id}/eventsList a fine-tuning job's events
GET/v1/fine_tuning/jobs/{fine_tuning_job_id}/checkpointsList a fine-tuning job's checkpoints
GET/v1/fine_tuning/jobs/{fine_tuning_job_id}Retrieve a fine-tuning job
POST/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancelCancel a fine-tuning job
POST/v1/fine_tuning/jobs/{fine_tuning_job_id}/pausePause a fine-tuning job
POST/v1/fine_tuning/jobs/{fine_tuning_job_id}/resumeResume a fine-tuning job

custom_llm_provider selects the provider entry, or you pass a managed job id and the gateway routes the call to the deployment that created the job. Create reads it from the JSON body. Retrieve, list, events and checkpoints read it from the query (?custom_llm_provider=openai), and cancel, pause and resume accept it in the JSON body or the query. Retrieve and list accept only openai and azure.

Settings​

SettingWhereDefaultWhat it does
finetune_settingstop level of config.yamlunsetThe provider credentials fine-tuning calls use, one entry per custom_llm_provider. With it unset, every call that names a provider is refused with 400. An entry without api_key uses that provider's usual environment variables: OPENAI_API_KEY, OPENAI_BASE_URL or OPENAI_API_BASE and OPENAI_ORGANIZATION for OpenAI, AZURE_OPENAI_API_KEY or AZURE_API_KEY with AZURE_API_BASE and AZURE_API_VERSION for Azure, and VERTEXAI_PROJECT, VERTEXAI_LOCATION and VERTEXAI_CREDENTIALS for Vertex AI. Change it when you add or rotate a provider account.
files_settingstop level of config.yamlunsetThe same per provider credentials for uploading training files with custom_llm_provider.
require_managed_filesspanway_settingsfalseWhen on, every file, batch and fine-tuning call must use a gateway managed id, so nobody can reach another team's job or file by guessing its raw provider id. When off, raw provider ids are accepted and run under the shared provider credentials. Turn it on for any gateway that serves more than one team.
request_timeoutrequest body600 secondsHow long the gateway waits for the provider on one fine-tuning call.

Events, checkpoints, pause and resume​

OpenAI and Azure OpenAI jobs expose their event log and saved checkpoints, and can be paused and resumed.

curl "http://localhost:4000/v1/fine_tuning/jobs/ftjob-abc123/events?custom_llm_provider=openai&limit=10" \
-H "Authorization: Bearer $SPANWAY_API_KEY"

curl -X POST http://localhost:4000/v1/fine_tuning/jobs/ftjob-abc123/pause \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"custom_llm_provider": "openai"}'

events and checkpoints return an OpenAI list object with data and has_more, and accept after and limit for paging. pause and resume return the updated job, with status set to paused while the job is paused. Vertex AI jobs do not support these operations, and the gateway answers 400 with the providers that do.

Who may touch a job​

A raw provider job id such as ftjob-abc123 runs under the gateway's shared provider credentials. Events, checkpoints, retrieve and cancel do not check who created a raw id. Pause and resume refuse a raw id that the gateway recorded for another user or team, and allow a raw id the gateway has no record of. For multi-tenant deployments, set require_managed_files: true under spanway_settings so that every call must use a managed job id, which is always checked against the user or team that created it. Managed files keeps training files private to their owner.

Who changed what​

Creating, cancelling, pausing and resuming a job writes an audit row with the key, user and team that did it, the job id, the operation, the provider, the job's model and its status after the change. The training file content is never stored. Read the rows on the Logs page, Audit Logs tab, or through GET /audit, see audit logs. Retrieve, list, events and checkpoints only read and write no audit row.

Errors​

StatusMessageWhat it meansFix
403Fine-tuning needs a Spanway enterprise licenseThe gateway has no valid license.Load a license, see enterprise.
422{"detail": [{"type": "missing", "loc": ["body", "training_file"], "msg": "Field required"}]}model or training_file is missing from the create body.Send both fields.
400Fine-tuning through a provider needs a finetune_settings block in config.yaml, and this gateway has none.No finetune_settings is configured.Add the block and restart.
400No finetune_settings entry in config.yaml matches custom_llm_provider='azure'. Add one with that provider's credentials, or use a Spanway managed file or job id.The call names a provider without an entry.Add an entry for that provider.
400Invalid request, No spanway managed file id or custom_llm_provider provided.A raw file or job id was sent with no provider.Send custom_llm_provider or use the managed id.
400Spanway doesn't support vertex_ai for 'cancel_fine_tuning_job'. Only 'openai' and 'azure' are supported.The provider does not support that operation through the gateway.Use OpenAI or Azure OpenAI, or the provider's console.
400Raw provider fine-tuning job ids cannot be used when require_managed_files is enabled in spanway_settings. Use the Spanway managed fine-tuning job id returned when the fine-tuning job was created.require_managed_files is on and a raw job id (or a raw file id, with file in the message) was sent.Use the managed id the gateway returned.
400This fine-tuning job on the openai fine-tuning API ...The data policy of the key, team or organization forbids sending training data to that provider.See data policy routing.
400target_model_names on list fine-tuning jobs must be a list of one model name. Example: ['gpt-4o']List was sent more than one model name.Send exactly one.
403User <user_id> does not have access to <job id>The managed job, or the raw id the gateway recorded on pause or resume, belongs to another user or team.Use a job your user or team created.
422{"detail": [{"type": "literal_error", "loc": ["query", "custom_llm_provider"], "msg": "Input should be 'openai' or 'azure'", ...}]}Retrieve or list was called with vertex_ai.Follow Vertex AI jobs in the Google Cloud console.
404NotFoundErrorThe job id is unknown to the provider.Check the id and the provider.
400provider error on the training fileThe JSONL file does not match the provider's format.Validate the file against the provider's fine-tuning format.

Logs​

LevelLineMeaningAction
ERRORspanway.proxy.proxy_server.create_fine_tuning_job(): Exception occurred - ...A create call failed. The same line exists for retrieve_fine_tuning_job, list_fine_tuning_jobs and cancel_fine_tuning_job.Read the error after the dash. A 400 here is the caller's mistake, anything else is worth checking.
ERRORspanway.proxy.fine_tuning_endpoints.apause_fine_tuning_job(): Exception occurred - ...An events, checkpoints, pause or resume call failed, named by its operation.As above.
ERRORgot exception in create_fine_tuning_job=...The provider call inside create failed.Check the provider credentials in finetune_settings and the provider status.