Azure AI Fine-Tuning Sessions SDK: Setup Guide (Preview)
The Azure AI Fine-Tuning Sessions SDK (preview) lets you run interactive supervised and reinforcement fine-tuning from Python, one session at a time. Read the preview limits before you build on it.
In This Article
What the SDK does
azure-ai-finetuningsessions is a preview client library for Programmed Training in Microsoft Foundry. It supports interactive supervised and reinforcement fine-tuning through fine-tuning sessions. You create a session, submit training and sampling requests, and save checkpoints, using either a synchronous or an asynchronous Python client.
This is a preview release (1.0.0b2). Availability of fine-tuning sessions and of individual features depends on your account, region, model, and service configuration. Installing the SDK does not enable a feature or grant model access.
Prerequisites
- Python 3.10 or later.
- An Azure subscription.
- A Foundry project with access to fine-tuning sessions and compatible capacity.
- The project endpoint, for example
https://<account>.services.ai.azure.com/api/projects/<project>.
Install the package
The package is published only as a pre-release, so the --pre flag is required:
python -m pip install --pre azure-ai-finetuningsessions
python -m pip install azure-identityIf you previously installed the older azure-ai-finetuning-sessions distribution (with hyphens), uninstall it first and update your dependency and import declarations. The current distribution is azure-ai-finetuningsessions, and imports use azure.ai.finetuningsessions.
Authenticate and create a session
Use an Azure Identity credential such as DefaultAzureCredential. Session creation requires an explicit LoRA configuration with an adapter rank. No default rank is supplied, and an empty LoRA configuration is not accepted. Use a base model and configuration your account supports.
from azure.ai.finetuningsessions import FineTuningSession, FineTuningSessionClient
from azure.ai.finetuningsessions.models import LoRAConfig
from azure.identity import DefaultAzureCredential
endpoint = "https://<account>.services.ai.azure.com/api/projects/<project>"
with DefaultAzureCredential() as credential:
with FineTuningSessionClient(endpoint=endpoint, credential=credential) as client:
session = FineTuningSession.create(
client,
base_model="<supported-base-model>",
lora_config=LoRAConfig(rank=16),
user_metadata={"experiment": "example"},
)
try:
checkpoint = session.save_weights_for_sampler(
sampling_session_seq_id=0,
seq_id=0,
)
print(checkpoint.checkpoint_id)
finally:
session.close()FineTuningSession.create(...) waits for initialization and returns a session wrapper. Its background heartbeat uses a thread. Calling session.close() stops that heartbeat and requests session unload. Closing the HTTP client alone does not unload the session or stop a synchronous wrapper's heartbeat.
Use the asynchronous client
The asynchronous client returns a session ID from await client.create_session(...), and its methods accept that ID:
from azure.ai.finetuningsessions.aio import FineTuningSessionClient
from azure.ai.finetuningsessions.models import LoRAConfig
from azure.identity.aio import DefaultAzureCredential
async def run():
async with DefaultAzureCredential() as credential:
async with FineTuningSessionClient(
endpoint="https://<account>.services.ai.azure.com/api/projects/<project>",
credential=credential,
) as client:
session_id = await client.create_session(
base_model="<supported-base-model>",
lora_config=LoRAConfig(rank=16),
)
try:
checkpoint_task = await client.save_weights_for_sampler_async(
session_id,
name="example-sampler",
)
checkpoint = await checkpoint_task
print(checkpoint.checkpoint_id)
finally:
await client.close_session(session_id)Closing the async client drains its heartbeat tasks and closes the transport, but does not unload or delete remote sessions, so call close_session explicitly. Keep the event loop responsive so background heartbeats can run. save_weights_for_sampler_async returns a task after submission: await both the method and its returned task before using the checkpoint.
Training and sampling operations
Use the convenience methods for complete workflows:
| Operation | Synchronous API | Asynchronous API |
|---|---|---|
| Forward/backward | session.forward_backward(batch) | await client.forward_backward(session_id, batch) |
| Forward only | session.forward(batch) | await client.forward(session_id, batch) |
| Optimizer step | session.optim_step(adam_params) | await client.optim_step(session_id, adam_params) |
| Save training state | session.save_weights(path) | await client.save_weights(session_id, path) |
| Sample | session.sample(...) | await client.sample(session_id, ...) |
Forward-only passes do not accumulate gradients. A request is submitted and then polled until its result is available. Successful submission does not mean the work has completed.
Text-only sampling and reasoning effort
Sampling and training convenience methods accept text_only_input instead of token-native inputs. Sampling accepts public system, user, assistant, and tool messages and optional function definitions. Only function tools are supported. Training accepts opaque sample_ref values and finite scalar advantages. Keep references opaque and use the exact returned value for subsequent training.
For compatible models, reasoning_effort accepts "low", "medium", "high", or None. Omitting it or passing None keeps the model default, and unsupported values are rejected before submission. Text-only sampling does not support prompt log-probabilities, caller-selected stopping criteria, or response_format.
Download checkpoint files
client.checkpoints.download_weights(session_id, checkpoint_id, output_path) and its async counterpart prepare an authorized checkpoint export, validate the file inventory, and stream the files to output_path/<session_id>/<checkpoint_id>:
client.checkpoints.download_weights(
session_id="session_example",
checkpoint_id="sampler_checkpoint",
output_path="./checkpoints",
)The checkpoint directory must be empty or absent. If a download fails or is cancelled, partial files are kept and nothing is resumed, deleted, or overwritten automatically: choose a new empty destination, or remove the partial checkpoint yourself before retrying. Downloading requires service support, an eligible model, and authorized checkpoint access. It does not grant access to base-model weights.
Troubleshooting
- Training engine interruptions.
TrainingEngineError(alsoEngineDeadError) with codeengine_deadorworker_crashedis a terminal interruption. Resume in a new session from the latest completed training checkpoint withFineTuningSession.create_from_checkpoint(...)orawait async_client.create_session_from_checkpoint(...). Keep the same base model and LoRA configuration. Restore the dataset cursor and learning-rate schedule yourself, because the SDK does not. Do not resubmit training on the interrupted session. - Out-of-memory and timeouts.
engine_oomandengine_timeoutdo not by themselves mean the trainer is terminally interrupted. Follow the error's guidance, such as reducing batch size or sequence length. - Retries. Default transport policies do not retry POST requests, including heartbeats. The SDK does not guarantee deduplication or exactly-once execution.
- Logging. Normal INFO logs contain progress and identifiers, not full request or response bodies. Setting
FINETUNING_VERBOSE_HTTP=1enables body logging, so do not enable it for sensitive data.
Source and further reading
This guide is based on the package documentation for azure-ai-finetuningsessions 1.0.0b2. For service guidance, see the Microsoft Foundry fine-tuning overview. Report SDK problems on the Azure SDK for Python issue tracker.