Azure AI Fine-Tuning Sessions client library for Python - version 1.0.0b3

Preview client library for Programmed Training in Microsoft Foundry, supporting interactive supervised and reinforcement fine-tuning through fine-tuning sessions. Create sessions, submit training and sampling requests, and save checkpoints with synchronous or asynchronous Python clients.

For general background, see the Microsoft Foundry fine-tuning overview. Availability of fine-tuning sessions and individual features depends on your account, region, model, and service configuration.

Getting started

Install the package

python -m pip install --pre azure-ai-finetuningsessions
python -m pip install azure-identity

Prerequisites

  • Python 3.10 or later.
  • An Azure subscription.
  • A Foundry project with access to fine-tuning sessions and compatible capacity.
  • The project endpoint supplied for the service, for example https://<account>.services.ai.azure.com/api/projects/<project>.

Preview access does not grant approval for restricted operations. This guide covers service-side checkpoint saves, metadata inspection, and resume; additional allowlisted operations require separate explicit service approval.

Authenticate and create a session

Use an Azure Identity credential such as DefaultAzureCredential. Session creation requires an explicit LoRA configuration and adapter rank. Use a base model and configuration supported by your account.

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()

AzureKeyCredential is also supported when API-key authentication is enabled for the endpoint. Use HTTPS. Default API-key authentication sends credentials only to the configured origin. For local testing, allow_insecure_http=True allows HTTP only for the configured loopback endpoint; it does not permit remote plaintext authentication.

Key concepts

  • A session holds model and adapter state between requests.
  • A request is submitted and observed until its result is available. Successful submission does not mean the requested work has completed.
  • A checkpoint saves training state or sampler weights. Sampling requires a completed sampler checkpoint identifier.
  • A heartbeat refreshes an active session during gaps between requests. Convenience creation starts background heartbeats automatically.

training_type accepts TrainingType members or strings. Known values are GlobalStandard, DatazoneStandard, and DeveloperTier. If omitted, the SDK sends no tier and the service selects the default. Explicit choices remain subject to service eligibility and capacity; the SDK does not select a different tier when the requested one is unavailable.

Examples

Client and session lifetime

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.

The asynchronous client returns a session ID from await client.create_session(...). 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 completion observers and heartbeat tasks and closes the transport, but does not unload or delete remote sessions. Use close_session explicitly. Keep the event loop responsive so background heartbeats can run.

The async save_weights_for_sampler_async method returns a task after submission. Await both the method and its returned task before using the checkpoint, as shown above.

session.delete() and await async_client.delete_session(session_id) stop the associated heartbeat and request deletion of the session and its resources. They treat an already absent session as successfully deleted. Deletion differs from unloading and does not wait for background storage cleanup.

Async connection pool sizing

The async client keeps Azure Core's default transport settings unless you pass connection_pool_limit=<positive integer> to its constructor. This sizes the default connection pool when it is opened; it does not change sampling or submission concurrency limits and does not guarantee a throughput improvement. The default async transport requires aiohttp; install it with python -m pip install aiohttp if it is not already available.

Boolean, non-integer, and non-positive limits are rejected. The option cannot be combined with a supplied transport, pipeline, or session. Without it, caller-supplied settings and ownership remain unchanged. Synchronous clients reject this async-only option.

Attach an existing session

For synchronous use, FineTuningSession(client, session_id) wraps an existing session and starts its heartbeat. Repeated wrappers own separate heartbeat threads, so manage their lifetimes explicitly.

For asynchronous use, call await async_client.attach_session(session_id). It registers one best-effort heartbeat per client/session and returns None. The first heartbeat is sent after 30 seconds; registration itself makes no HTTP request and does not verify existence, readiness, or access. Client close stops these tasks without unloading the remote session.

Training and sampling

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. Training and sampling helpers submit work, retrieve completed results, normalize them, and apply their documented batching and error-handling behavior.

ModelInput accepts ordered text and image chunks. ModelInputChunk(tokens=...) represents token IDs; ImageChunk accepts validated JPEG, PNG, or WebP data. Image validation checks the 10,000,000-byte limit, matching content and format, positive expected_tokens, and at most 64 images per example. Legacy token-only mappings remain accepted; unknown explicit chunk tags are not reclassified. Support for any particular chunk variant still depends on the service.

Set AZURE_AI_FINETUNING_MAX_CHUNK_BYTES before importing the SDK to override the approximate per-request chunk-size budget. Unset or invalid values retain the 5,000,000-byte default; invalid values log a warning. This does not change the service's request limits.

SamplingParams.response_format requests a response format from compatible models and providers. SamplingOperationResult is an alias for SampleOperationResult. Its read-only prompt_tokens reports the service's prompt count once per prompt, not once per sampled sequence. Missing or invalid counts remain None; the SDK does not estimate them from other data.

Experimental native losses

LossFn.TIS selects sum-reduced token-level truncated importance sampling. Its clip_high_threshold must be finite and at least 1; the service defaults to 2 when it is omitted. TIS is also accepted as "tis" for reference-based text-only training. Selecting it emits an experimental warning.

Native LossFn.SCORE_CENTERING and sampling head capture require an explicit client opt-in. Import ExperimentalSettings from azure.ai.finetuningsessions.experimental and pass experimental_settings=ExperimentalSettings(enable_score_centering=True) to either client constructor. This also emits an experimental warning when used; constructing the client alone does not warn.

SamplingParams.score_centering_head_size accepts 0 to disable capture or 1 through 128 to request decode-time top-K heads. Capture requires temperature=1, top_p=1, top_k=-1, and no response_format; it is not supported with text-only input. Native training accepts corresponding head tokens, log-probabilities and masks in LossFnInputs. TensorData.shape preserves multidimensional shapes while values remain flat on the wire. Returned SampledSequence objects preserve head data and capture provenance.

These features are under development and require compatible service support. The SDK opt-in does not enable service capabilities, grant authorization, or guarantee deployment availability. Existing losses are unchanged.

Completion-feed sampling

Synchronous and asynchronous convenience session creation advertise completion-feed support. Sampling uses server-sent events (SSE) only when the service negotiates the exact enablement value true. Missing or other values, older services, and attached sessions without that negotiation retain legacy polling. Raw operations, training, and session initialization keep their existing polling behavior.

session.sample() remains blocking, and await async_client.sample(...) returns the same result models as before. Notifications trigger retrieval of the persisted result or error; they are not generated tokens or final results. Stream reconnects preserve progress without resubmitting sampling. A persisted failure triggers resubmission only when the service authorizes it. Ordinary submission retries are unchanged and do not guarantee deduplication if a reply is lost. Typed sampling errors that include a session ID report the canonical session_ ID, even when requests use a different resource ID.

Short socket timeouts in the synchronous observer apply only to completion streams and acknowledgements. Sample submission and result retrieval use their normal request timeouts. Missed notifications are recovered by checking for a result after 10 minutes, then 5 minutes after each pending response. Feed outages keep that sparse schedule rather than switching to frequent polling. Existing heartbeats continue independently.

When sampling becomes idle, an open stream can finish its current bounded window and be reused by new sampling in that window. Client close stops observation and drains owned tasks before closing the transport. If a synchronous close times out waiting for observation or its workers, it reports that shutdown is still in progress. Cleanup continues on the observer thread after outstanding requests finish, and repeated calls wait for the same cleanup. Caller-owned transport resources and credentials must remain available until cleanup finishes. A synchronous observation cleanup error does not replace an exception already raised in the client's with block. The close timeout cannot stop blocking I/O or guarantee that Python exits by a deadline. Canceling a local sampling call or closing the client does not cancel service-side inference or unload the session.

Use one logical consuming client per session. Observation progress is held in client memory; cross-process acknowledgement sharing and restoration after process restart are not supported. INFO progress updates count cumulative final sampling-call outcomes for each session on that client, not notifications or intermediate retries. Totals survive reconnects and idle periods and start fresh with a new client.

Opt-in text-only operations

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 without exposing underlying token evidence.

Text-only sampling results contain valid assistant choices or safely classified invalid choices. Invalid choices have no public message but can still have trainable references; they are not automatically operation failures. Keep references opaque and use the exact returned value for subsequent training. Reference training exposes scalar metrics rather than private per-token output.

For compatible models, reasoning_effort accepts "low", "medium", "high", or None. Omission or None preserves the model default. Unsupported values are rejected before submission. Text-only sampling does not support prompt log-probabilities, caller-selected stopping criteria, or response_format. Installing the SDK does not enable these capabilities or grant model access.

Checkpoint status and publication

Checkpoint listings can include pending or failed save reservations as well as completed checkpoints. checkpoint.time is a datetime or None; handle None before formatting, comparing, or sorting timestamps. status describes the save, not approval for deployment.

publication contains typed CheckpointPublicationState metadata. publication_required and deployment_eligible describe separate deployment requirements. Metadata omitted by older services remains None and must not be treated as deployment approval.

Raw operations and convenience methods

The raw operation groups provide lower-level access with explicit request models and per-call API versions. Generated requests use canonical /fine_tuning_sessions routes. Some maintained convenience paths retain compatibility routes using /fine_tuning/sessions; use a service endpoint that supports the operations you call rather than manually rewriting URLs.

Raw create methods return submission results without waiting for initialization. Default raw begin_* methods return pollers that accept HTTP 200 submissions and poll the returned request ID. They do not start a heartbeat. Use poller.result() or await poller.result() to obtain completion. Explicit custom polling or polling=False retains the chosen strategy's semantics; disabling polling does not confirm that work completed.

Raw client.training.forward and client.sessions.delete methods are not provided in this preview. Use their session/convenience equivalents.

Troubleshooting

Training engine interruptions

TrainingEngineError (also available as EngineDeadError) with code engine_dead or worker_crashed indicates a terminal interruption. Resume in a new session from the latest completed training checkpoint using FineTuningSession.create_from_checkpoint(...) or await async_client.create_session_from_checkpoint(...).

Keep the source base model and LoRA configuration. Restore the dataset cursor and client-side learning-rate schedule separately; the SDK does not restore them. Progress after the checkpoint is not recovered. Without a completed training checkpoint, restart from the base model.

Do not resubmit training on the interrupted session. Terminal interruption errors remain terminal even if a response includes a retry hint. session.close() and await async_client.close_session(session_id) also preserve known typed service errors and the canonical session ID. An engine_dead HTTP 409 raises TrainingEngineError; it does not confirm unload. Unrecognized conflicts and malformed error bodies remain HTTP errors rather than being inferred as engine death. engine_oom and engine_timeout do not by themselves mean the trainer is terminally interrupted; follow the error's guidance, such as reducing batch size or sequence length for an out-of-memory error.

Transport, retries, and logging

Default transport policies do not retry POST requests, including heartbeats. Convenience operations have separate bounded recovery for supported service responses. An explicit retry_policy remains caller-owned; the SDK does not guarantee deduplication or exactly-once execution. Default raw pollers disable transport retries and redirects for their own submission and polling requests.

Default API-key authentication is removed on cross-origin redirects. Callers supplying custom policies or pipelines are responsible for their authentication, retry, and redirect configuration.

Normal INFO logs contain progress and identifiers rather than complete request or response bodies. FINETUNING_VERBOSE_HTTP=1 enables body logging; do not enable it for sensitive data. Avoid logging credentials, training inputs, generated messages, or checkpoint contents.

Compatibility and migration

The distribution is azure-ai-finetuningsessions and imports use azure.ai.finetuningsessions, including its aio namespace. If an older azure-ai-finetuning-sessions distribution is installed, uninstall it before installing this package and update dependency/import declarations.

Creation and checkpoint-resume calls require lora_config=LoRAConfig(rank=...). No implicit rank or empty LoRA configuration is supplied.

The obsolete FoundryFeaturesOptInKeys.AGENT_INSIGHTS_V1_PREVIEW constant is removed in b2 because Agent Insights no longer requires that preview opt-in. Explicit references must be removed. Fine-tuning continues using the unchanged Foundry-Features: FineTuningSessions=V1Preview value.

Next steps

Review the Microsoft Foundry fine-tuning overview for service guidance. For questions or bug reports, use the Azure SDK for Python issue tracker.

Contributing

See the Azure SDK for Python contribution guide and Microsoft Open Source Code of Conduct.