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

# Create a clinical summary for a given patient

> Starts summary generation workflow and streams workflow events as SSE.

Streaming results are forwarded as Server-Sent Events (SSE) with each event containing a JSON payload.

```json
{
  "data": {
    "type": "text-delta",
    "payload": {
      "sectionTitle": "History of Present Illness",
      "sectionIndex": 0,
      "text": "Patient presents..."
    }
  },
  "id": "5"
}
```

In this implementation, the SSE `event` field is omitted. Differentiate event kinds using `data.type`.

Raw SSE framing over the wire looks like:

```text
id: 5
data: {"type":"text-delta","payload":{"sectionTitle":"History of Present Illness","sectionIndex":0,"text":"Patient presents..."}}
```

### Possible Payload shapes

There can be multiple `text-start` events, one per section of the summary template. Each `text-start` event will be followed by one or more `text-delta` events with the same sectionTitle and sectionIndex,
and eventually a `text-end` event.

```
{
  "type": "text-start",
  "payload": {
    "sectionTitle": string,
    "sectionIndex": number,
  }
}
```

```
{
  "type": "text-delta",
  "payload": {
    "sectionTitle": string,
    "sectionIndex": number,
    "text": string, // this is a delta, so you should append it to the previous text for this section
  }
}
```

```
{
  "type": "text-end",
  "payload": {
    "sectionTitle": string,
    "sectionIndex": number,
  }
}
```

After all sections have been streamed, there will be a `summary-end` event indicating that the summary generation workflow has completed. If the status field in the payload is `success`,
the summary has been persisted and can be retrieved with the provided summaryId. If the status is `error`, the summary generation has failed and the error message will be included in the payload.

```
{
  "type": "summary-end",
  "payload": {
    "status": "success",
    "summaryId": string,
  }
}

OR

{
  "type": "summary-end",
  "payload": {
    "status": "error",
    "error": string,
  }
}
```

### Example SSE Stream

```
id: 1
data: {"type":"text-start","payload":{"sectionTitle":"History of Present Illness","sectionIndex":0}}

id: 2
data: {"type":"text-delta","payload":{"text":"Patient reports progressive shortness of breath over 3 days.","sectionTitle":"History of Present Illness","sectionIndex":0}}

id: 3
data: {"type":"text-start","payload":{"sectionTitle":"Medications","sectionIndex":1}}

id: 4
data: {"type":"text-delta","payload":{"text":"Lisinopril 10 mg once daily.","sectionTitle":"Medications","sectionIndex":1}}

id: 5
data: {"type":"text-delta","payload":{"text":"No chest pain, fever, or recent travel were reported.","sectionTitle":"History of Present Illness","sectionIndex":0}}

id: 6
data: {"type":"text-delta","payload":{"text":"Metformin 500 mg twice daily with meals.","sectionTitle":"Medications","sectionIndex":1}}

id: 7
data: {"type":"text-end","payload":{"sectionTitle":"History of Present Illness","sectionIndex":0}}

id: 8
data: {"type":"text-end","payload":{"sectionTitle":"Medications","sectionIndex":1}}

id: 9
data: {"type":"summary-end","payload":{"status":"success","summaryId":"sum_01JABZX3V4Q2P2M41ME6E3FQ7R"}}


```


## OpenAPI

````yaml /api-reference/hermes.yml post /ai/v1/summaries
openapi: 3.1.0
info:
  title: AI Engine
  description: Turn data into health intelligence
  version: 1.0.0
servers:
  - url: https://api.{workspaceId}.clinia.cloud
    variables:
      workspaceId:
        default: ''
        description: The workspace id.
security: []
paths:
  /ai/v1/summaries:
    post:
      tags:
        - Summaries
      summary: Create a clinical summary for a given patient
      description: Starts summary generation workflow and streams workflow events as SSE.
      parameters:
        - in: header
          name: x-user-id
          schema:
            type: string
          required: false
          description: >-
            The ID of the user making the request. Used to associate the request
            with a specific user.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                patientId:
                  type: string
                  excludeFromTrace: pii
                  description: >-
                    The patient id that we want to create a clinical summary
                    for. This should be the same patient id used during
                    ingestion.
                summaryTemplateId:
                  type: string
                  description: >-
                    ID of a summary template. This can be retrieved via `GET
                    /summary-templates`.
              required:
                - patientId
                - summaryTemplateId
      responses:
        '200':
          description: Summary workflow event stream
          content:
            text/event-stream:
              schema:
                anyOf:
                  - type: object
                    properties:
                      id:
                        type: string
                      event:
                        type: string
                      data:
                        allOf:
                          - $ref: '#/components/schemas/textDelta'
                          - type: object
                            properties:
                              payload:
                                type: object
                                properties:
                                  text:
                                    type: string
                                  sectionTitle:
                                    type: string
                                    description: >-
                                      The title of the section from the summary
                                      template that this text belongs to.
                                  sectionIndex:
                                    type: number
                                    minimum: 0
                                    description: >-
                                      The index of the section from the summary
                                      template that this text belongs to. This
                                      is used to order the sections in the final
                                      summary and to facilitate incremental
                                      rendering in multiple sections at once.
                                required:
                                  - text
                                  - sectionTitle
                                  - sectionIndex
                    required:
                      - data
                  - type: object
                    properties:
                      id:
                        type: string
                      event:
                        type: string
                      data:
                        allOf:
                          - $ref: '#/components/schemas/textStart'
                          - type: object
                            properties:
                              payload:
                                type: object
                                properties:
                                  sectionTitle:
                                    type: string
                                    description: >-
                                      The title of the section from the summary
                                      template that this text belongs to.
                                  sectionIndex:
                                    type: number
                                    minimum: 0
                                    description: >-
                                      The index of the section from the summary
                                      template that this text belongs to. This
                                      is used to order the sections in the final
                                      summary and to facilitate incremental
                                      rendering in multiple sections at once.
                                required:
                                  - sectionTitle
                                  - sectionIndex
                            required:
                              - payload
                    required:
                      - data
                  - type: object
                    properties:
                      id:
                        type: string
                      event:
                        type: string
                      data:
                        allOf:
                          - $ref: '#/components/schemas/textEnd'
                          - type: object
                            properties:
                              payload:
                                type: object
                                properties:
                                  sectionTitle:
                                    type: string
                                    description: >-
                                      The title of the section from the summary
                                      template that this text belongs to.
                                  sectionIndex:
                                    type: number
                                    minimum: 0
                                    description: >-
                                      The index of the section from the summary
                                      template that this text belongs to. This
                                      is used to order the sections in the final
                                      summary and to facilitate incremental
                                      rendering in multiple sections at once.
                                required:
                                  - sectionTitle
                                  - sectionIndex
                            required:
                              - payload
                    required:
                      - data
                  - type: object
                    properties:
                      id:
                        type: string
                      event:
                        type: string
                      data:
                        type: object
                        properties:
                          type:
                            type: string
                            enum:
                              - summary-end
                          payload:
                            oneOf:
                              - type: object
                                properties:
                                  status:
                                    type: string
                                    enum:
                                      - success
                                  summaryId:
                                    type: string
                                    description: >-
                                      ID with which you can retrieve the
                                      persisted summary via GET
                                      /summaries/:summaryId. This id can also
                                      later be retrieved via GET /summaries with
                                      the patientId.
                                required:
                                  - status
                                  - summaryId
                              - type: object
                                properties:
                                  status:
                                    type: string
                                    enum:
                                      - error
                                  error:
                                    type: string
                                required:
                                  - status
                                  - error
                                description: >-
                                  In case of an error, the summary will not be
                                  persisted and will not be retrievable.
                        required:
                          - type
                          - payload
                    required:
                      - data
        '400':
          description: Invalid request parameters
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                code: 400
                type: INVALID_ARGUMENT
                message: Invalid request parameters
        '401':
          description: Authentication required
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                code: 401
                type: UNAUTHENTICATED
                message: Unauthenticated
        '403':
          description: Permission denied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                code: 403
                type: PERMISSION_DENIED
                message: Permission denied
        '404':
          description: Resource not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                code: 404
                type: NOT_FOUND
                message: Resource not found
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                code: 500
                type: INTERNAL
                message: Internal Server Error
components:
  schemas:
    textDelta:
      type: object
      properties:
        type:
          type: string
          enum:
            - text-delta
        payload:
          type: object
          properties:
            text:
              type: string
          required:
            - text
      required:
        - type
        - payload
    textStart:
      type: object
      properties:
        type:
          type: string
          enum:
            - text-start
      required:
        - type
    textEnd:
      type: object
      properties:
        type:
          type: string
          enum:
            - text-end
      required:
        - type
    ApiError:
      type: object
      properties:
        code:
          type: number
          exclusiveMinimum: 0
        type:
          type: string
          enum:
            - ALREADY_EXISTS
            - FAILED_PRECONDITION
            - INTERNAL
            - INVALID_ARGUMENT
            - NOT_FOUND
            - OUT_OF_RANGE
            - UNIMPLEMENTED
            - UNAUTHENTICATED
            - PERMISSION_DENIED
            - CONTENT_TOO_LARGE
            - UNSUPPORTED_MEDIA_TYPE
        message:
          type: string
      required:
        - code
        - type
        - message

````