---
id: embedding
title: Embedding
hide_title: false
hide_table_of_contents: false
description: How to use the SAP Cloud SDK for AI to perform embedding tasks using Azure OpenAI models through SAP AI Core.
keywords:
  - sap
  - cloud
  - sdk
  - ai
  - embedding
  - openai
---

Initialize the `AzureOpenAiEmbeddingClient` by following the instructions in the [Client Initialization](../../foundation-models#client-initialization) section.

Currently, the client sends request with Azure OpenAI API version `2024-10-21`.
We are continuously updating the client to match the latest API specification.
You can overwrite the API version by setting the `api-version` parameter in the `CustomRequestConfig` object.
Refer to the [Custom Request Configuration](../../foundation-models#custom-request-configuration) section for more details.

## Making Requests

```ts
const response = await client.run({
  input: 'AI is fascinating'
});
const embedding = response.getEmbedding();
```

### Access to Raw HTTP Response

The SAP Cloud SDK for AI provides access to the underlying HTTP response through the `rawResponse` property on response objects.
Use this for advanced scenarios such as accessing response headers, inspecting HTTP status codes, or debugging.

```ts
const response = await client.run({
  input: 'AI is fascinating'
});
// Recommended: Use convenience methods for typical use cases
const embedding = response.getEmbedding();

// Advanced: Access HTTP response details when needed
console.log(response.rawResponse.status); // HTTP status code
console.log(response.rawResponse.headers); // Response headers
console.log(response.rawResponse.data); // Raw response data
```

:::tip
For most use cases, prefer the convenience methods like `getEmbedding();`.
Use `rawResponse` only when you need direct access to HTTP-level details.
:::

The `rawResponse` property is available on embedding response objects.

## Resilience

Use the `resilience()` function from `@sap-cloud-sdk/resilience` to add resilience to requests.
By default, it enables a circuit breaker and a 10-second timeout.

```ts
import { resilience } from '@sap-cloud-sdk/resilience';

const response = await client.run(
  { input: 'What is the capital of Germany?' },
  { middleware: resilience() }
);
```

:::tip
`resilience()` returns an array of middleware.
You can pass it directly to `middleware` or combine it with other middleware: `[...resilience(), myMiddleware]`.
:::

Customize the behavior by passing options:

<!-- vale off -->

```ts
const response = await client.run(
  { input: 'What is the capital of Germany?' },
  {
    middleware: resilience({
      timeout: 5000, // 5 seconds; true for default 10s, false to disable
      circuitBreaker: true, // true by default, false to disable
      retry: 3 // false by default; true for 3 retries, or pass a number
    })
  }
);
```

<!-- vale on -->

For advanced resilience patterns, refer to the [SAP Cloud SDK documentation on resilience](https://sap.github.io/cloud-sdk/docs/js/guides/resilience).

### Request Cancellation

Cancel HTTP requests using the `AbortSignal` API to stop in-flight requests when they are no longer needed.
This is useful when end-users navigate away from a page, close a dialog, or explicitly cancel an operation.
Pass an `AbortSignal` through the `signal` property of the request configuration parameter and call the `abort()` method on the controller when you want to cancel the request.

```ts
const controller = new AbortController();

const request = {
  input: 'What is the capital of Germany?'
};

const response = await client.run(request, {
  signal: controller.signal
});

// Cancel the request after one second
setTimeout(() => {
  controller.abort();
}, 1000);

try {
  await response;
} catch (error) {
  console.error('Request was cancelled:', error.message);
}
```

:::note
HTTP request cancellation may not cancel the request on the AI service.
The cancellation stops the HTTP connection from the client side, but the service may continue processing the request.
Metering information may not be available for cancelled requests.
:::
