> ## Documentation Index
> Fetch the complete documentation index at: https://developer.suki.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Resolve a Remote Session Conflict

> End the blocking ambient session for an EMR encounter, then retry create

# Resolve a Remote Session Conflict

When another modality already has an active ambient session for the same encounter, create returns HTTP 409. Read the blocking session ID from the error, end that session, wait one second, then retry create.

## Problem

Create returns HTTP **409** (`session_in_progress`) when another modality already holds an active ambient session for the same `emr_encounter_id`. This happens when Mobile, Web SDK, or a previous API session was not ended before you retry create.

Read `ambient_session_id` from the conflict response, [End session](https://developer.suki.ai/api-reference/ambient-sessions/end.md), wait **1 second**, then retry create. If the error body omits the blocking session ID, end the active session from the owning modality first.

## Architecture

![Flowchart: POST /create, 409 conflict?, Session created, Read blocking session ID, POST /end, Wait 1 second, Retry POST /create.](https://mintcdn.com/suki-1e08f176/eryJVVKcBRotEMfQ/documentation/assets/diagrams/cookbooks-resolve-session-conflict-1-light.svg?fit=max\&auto=format\&n=eryJVVKcBRotEMfQ\&q=85\&s=12beb71617f0a09fa212ce4ace61469e)

```mermaid actions={false} theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#FFFBDE','primaryTextColor':'#1C1C1C','primaryBorderColor':'#FFD147','lineColor':'#6F5410','secondaryColor':'#FFFDF5','tertiaryColor':'#FFEB94','fontSize':'13px'}}}%%
flowchart LR
  A[POST /create] --> B{409 conflict?}
  B -->|no| C[Session created]
  B -->|yes| D[Read blocking session ID]
  D --> E[POST /end]
  E --> F[Wait 1 second]
  F --> G[Retry POST /create]
```

## Prerequisites

> [!TIP]
>
> Completed [Partner onboarding](https://developer.suki.ai/documentation/get-started/partner-onboarding.md) and have `partner_id` with a valid Partner Token flow.

> [!TIP]
>
> An `emr_encounter_id` shared across products using [Share one note across products](https://developer.suki.ai/documentation/cookbooks/pass-emr-encounter-id.md).

> [!TIP]
>
> `sdp_suki_token` and `sdp_provider_id` headers ready for create and end calls.

## Solution

> [card] Before
>
> Create with a shared `emr_encounter_id` ([Share one note across products](https://developer.suki.ai/documentation/cookbooks/pass-emr-encounter-id.md)).

> [card] This Recipe
>
> On 409, end the blocking session, wait 1 second, then retry create.

> [card] Next
>
> Stream on the new session, then [End Ambient after Streaming](https://developer.suki.ai/documentation/cookbooks/end-ambient-after-streaming.md).

Read `ambient_session_id` from the conflict response, [End session](https://developer.suki.ai/api-reference/ambient-sessions/end.md), wait **1 second**, then retry create.

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
> // resolve-session-conflict.ts
>
> const BASE_URL = process.env.SUKI_BASE_URL ?? "https://sdp.suki.ai";
> const SDP_SUKI_TOKEN = process.env.SDP_SUKI_TOKEN!;
> const SDP_PROVIDER_ID = process.env.SDP_PROVIDER_ID!;
>
> function createHeaders(): Record<string, string> {
>   return {
>     "Content-Type": "application/json",
>     sdp_suki_token: SDP_SUKI_TOKEN,
>     sdp_provider_id: SDP_PROVIDER_ID,
>   };
> }
>
> async function endAmbientSession(ambientSessionId: string): Promise<void> {
>   const endRes = await fetch(
>     `${BASE_URL}/api/v1/ambient/session/${ambientSessionId}/end`,
>     {
>       method: "POST",
>       headers: {
>         sdp_suki_token: SDP_SUKI_TOKEN,
>         sdp_provider_id: SDP_PROVIDER_ID,
>       },
>     }
>   );
>   if (!endRes.ok) {
>     throw new Error(`End ambient session failed: ${endRes.status}`);
>   }
> }
>
> async function createOrResolveConflict(emrEncounterId: string): Promise<{
>   ambient_session_id: string;
>   note_id: string;
> }> {
>   const createRes = await fetch(`${BASE_URL}/api/v1/ambient/session/create`, {
>     method: "POST",
>     headers: createHeaders(),
>     body: JSON.stringify({ emr_encounter_id: emrEncounterId }),
>   });
>
>   if (createRes.ok) {
>     return createRes.json();
>   }
>
>   const err = (await createRes.json().catch(() => ({}))) as {
>     message?: string;
>     ambient_session_id?: string;
>   };
>   const message = String(err?.message || "");
>   const isConflict =
>     createRes.status === 409 || message.includes("session_in_progress");
>
>   if (!isConflict) {
>     throw new Error(`Create ambient session failed: ${createRes.status}`);
>   }
>
>   const blockingSessionId = err.ambient_session_id;
>   if (!blockingSessionId) {
>     throw new Error(
>       "Conflict returned without ambient_session_id. End the active session from the owning modality, then retry."
>     );
>   }
>
>   await endAmbientSession(blockingSessionId);
>   await new Promise((r) => setTimeout(r, 1000));
>
>   const retry = await fetch(`${BASE_URL}/api/v1/ambient/session/create`, {
>     method: "POST",
>     headers: createHeaders(),
>     body: JSON.stringify({ emr_encounter_id: emrEncounterId }),
>   });
>   if (!retry.ok) {
>     throw new Error(`Retry create failed: ${retry.status}`);
>   }
>   return retry.json();
> }
>
> const emrEncounterId =
>   process.env.EMR_ENCOUNTER_ID ?? "6ec3920f-b0b1-499d-a4e9-889bf788e5ab";
> const created = await createOrResolveConflict(emrEncounterId);
> console.log(JSON.stringify(created, null, 2));
> ```

> [tab] Python
>
> ```python theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
> # resolve_session_conflict.py
> # pip install requests
>
> import json
> import os
> import time
>
> import requests
>
> BASE_URL = os.environ.get("SUKI_BASE_URL", "https://sdp.suki.ai")
> SDP_SUKI_TOKEN = os.environ["SDP_SUKI_TOKEN"]
> SDP_PROVIDER_ID = os.environ["SDP_PROVIDER_ID"]
>
>
> def create_headers() -> dict[str, str]:
>     return {
>         "Content-Type": "application/json",
>         "sdp_suki_token": SDP_SUKI_TOKEN,
>         "sdp_provider_id": SDP_PROVIDER_ID,
>     }
>
>
> def end_ambient_session(ambient_session_id: str) -> None:
>     end = requests.post(
>         f"{BASE_URL}/api/v1/ambient/session/{ambient_session_id}/end",
>         headers={
>             "sdp_suki_token": SDP_SUKI_TOKEN,
>             "sdp_provider_id": SDP_PROVIDER_ID,
>         },
>         timeout=30,
>     )
>     end.raise_for_status()
>
>
> def create_or_resolve_conflict(emr_encounter_id: str) -> dict:
>     create = requests.post(
>         f"{BASE_URL}/api/v1/ambient/session/create",
>         headers=create_headers(),
>         json={"emr_encounter_id": emr_encounter_id},
>         timeout=30,
>     )
>     if create.ok:
>         return create.json()
>
>     try:
>         err = create.json()
>     except ValueError:
>         err = {}
>
>     message = str(err.get("message") or "")
>     is_conflict = create.status_code == 409 or "session_in_progress" in message
>     if not is_conflict:
>         create.raise_for_status()
>
>     blocking = err.get("ambient_session_id")
>     if not blocking:
>         raise RuntimeError(
>             "Conflict returned without ambient_session_id. "
>             "End the active session from the owning modality, then retry."
>         )
>
>     end_ambient_session(blocking)
>     time.sleep(1)
>
>     retry = requests.post(
>         f"{BASE_URL}/api/v1/ambient/session/create",
>         headers=create_headers(),
>         json={"emr_encounter_id": emr_encounter_id},
>         timeout=30,
>     )
>     retry.raise_for_status()
>     return retry.json()
>
>
> emr_encounter_id = os.environ.get(
>     "EMR_ENCOUNTER_ID", "6ec3920f-b0b1-499d-a4e9-889bf788e5ab"
> )
> created = create_or_resolve_conflict(emr_encounter_id)
> print(json.dumps(created, indent=2))
> ```

**1. Detect the 409 conflict**

On create failure, check for HTTP 409 or a `session_in_progress` message in the response body.

**2. Read the blocking session ID**

Parse `ambient_session_id` from the error response. Confirm it before calling end.

**3. End the blocking session**

Call `POST /api/v1/ambient/session/{ambient_session_id}/end` with Partner Token headers. See [End session](https://developer.suki.ai/api-reference/ambient-sessions/end.md).

**4. Wait and retry create**

Wait **1 second** after end, then retry create with the same `emr_encounter_id`.

## Try it

> [card]
>
> * **Retry succeeds after ending the blocker**
>
>   After you end the blocking session and wait 1 second, the retry create call should return a new `ambient_session_id` and `note_id`.
>
> * **Missing session ID needs manual cleanup**
>
>   If the 409 body omits `ambient_session_id`, end the active session from the owning modality (Mobile or Web SDK), then retry create.
>
> * **Confirm the blocking ID before end**
>
>   Always verify `ambient_session_id` from the conflict response matches the session you intend to end before calling the end endpoint.

## Common mistakes

> [!WARNING]
>
> * Retry create immediately without ending the blocking session or waiting 1 second.
> * End a session without confirming the blocking `ambient_session_id` from the 409 response.
> * Assume a 409 will always include `ambient_session_id` in the error body.

## Related cookbooks

Ambient
API

### [Share One Note across Products](https://developer.suki.ai/documentation/cookbooks/pass-emr-encounter-id.md)

Share one note with emr\_encounter\_id.

5 min

Ambient
API

### [Wait for Ambient before Fetch](https://developer.suki.ai/documentation/cookbooks/wait-for-status-before-retrieve.md)

Poll until status is completed.

5 min

Run This Recipe

[Open Full Tutorial](https://developer.suki.ai/documentation/tutorials/ambient-websocket-code-example.md)
[View API Reference](https://developer.suki.ai/api-reference/ambient-sessions/end.md)

Step-by-step tutorial and API reference for this workflow.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.