Skip to content

release: 4.91.0 #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "4.90.0"
".": "4.91.0"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 82
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai%2Fopenai-6663c59193eb95b201e492de17dcbd5e126ba03d18ce66287a3e2c632ca56fe7.yml
openapi_spec_hash: 7996d2c34cc44fe2ce9ffe93c0ab774e
config_hash: 9351ea829c2b41da3b48a38c934c92ee
config_hash: e25e31d8446b6bc0e3ef7103b6993cce
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 4.91.0 (2025-03-31)

Full Changelog: [v4.90.0...v4.91.0](https://github.com/openai/openai-node/compare/v4.90.0...v4.91.0)

### Features

* **api:** add `get /responses/{response_id}/input_items` endpoint ([ef0e0ac](https://github.com/openai/openai-node/commit/ef0e0acd469379ae6f2745c83e6c6813ff7b4edc))


### Performance Improvements

* **embedding:** default embedding creation to base64 ([#1312](https://github.com/openai/openai-node/issues/1312)) ([e54530e](https://github.com/openai/openai-node/commit/e54530e4f6f00d7d74fc8636bbdb6f6280548750)), closes [#1310](https://github.com/openai/openai-node/issues/1310)

## 4.90.0 (2025-03-27)

Full Changelog: [v4.89.1...v4.90.0](https://github.com/openai/openai-node/compare/v4.89.1...v4.90.0)
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openai/openai",
"version": "4.90.0",
"version": "4.91.0",
"exports": {
".": "./index.ts",
"./helpers/zod": "./helpers/zod.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openai",
"version": "4.90.0",
"version": "4.91.0",
"description": "The official TypeScript library for the OpenAI API",
"author": "OpenAI <support@openai.com>",
"types": "dist/index.d.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,27 @@ export const toBase64 = (str: string | null | undefined): string => {
throw new OpenAIError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
};

/**
* Converts a Base64 encoded string to a Float32Array.
* @param base64Str - The Base64 encoded string.
* @returns An Array of numbers interpreted as Float32 values.
*/
export const toFloat32Array = (base64Str: string): Array<number> => {
if (typeof Buffer !== 'undefined') {
// for Node.js environment
return Array.from(new Float32Array(Buffer.from(base64Str, 'base64').buffer));
} else {
// for legacy web platform APIs
const binaryStr = atob(base64Str);
const len = binaryStr.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryStr.charCodeAt(i);
}
return Array.from(new Float32Array(bytes.buffer));
}
};

export function isObj(obj: unknown): obj is Record<string, unknown> {
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
}
42 changes: 40 additions & 2 deletions src/resources/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,47 @@ export class Embeddings extends APIResource {
*/
create(
body: EmbeddingCreateParams,
options?: Core.RequestOptions,
options?: Core.RequestOptions<EmbeddingCreateParams>,
): Core.APIPromise<CreateEmbeddingResponse> {
return this._client.post('/embeddings', { body, ...options });
const hasUserProvidedEncodingFormat = !!body.encoding_format;
// No encoding_format specified, defaulting to base64 for performance reasons
// See https://github.com/openai/openai-node/pull/1312
let encoding_format: EmbeddingCreateParams['encoding_format'] =
hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';

if (hasUserProvidedEncodingFormat) {
Core.debug('Request', 'User defined encoding_format:', body.encoding_format);
}

const response: Core.APIPromise<CreateEmbeddingResponse> = this._client.post('/embeddings', {
body: {
...body,
encoding_format: encoding_format as EmbeddingCreateParams['encoding_format'],
},
...options,
});

// if the user specified an encoding_format, return the response as-is
if (hasUserProvidedEncodingFormat) {
return response;
}

// in this stage, we are sure the user did not specify an encoding_format
// and we defaulted to base64 for performance reasons
// we are sure then that the response is base64 encoded, let's decode it
// the returned result will be a float32 array since this is OpenAI API's default encoding
Core.debug('response', 'Decoding base64 embeddings to float32 array');

return (response as Core.APIPromise<CreateEmbeddingResponse>)._thenUnwrap((response) => {
if (response && response.data) {
response.data.forEach((embeddingBase64Obj) => {
const embeddingBase64Str = embeddingBase64Obj.embedding as unknown as string;
embeddingBase64Obj.embedding = Core.toFloat32Array(embeddingBase64Str);
});
}

return response;
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '4.90.0'; // x-release-please-version
export const VERSION = '4.91.0'; // x-release-please-version
31 changes: 31 additions & 0 deletions tests/api-resources/embeddings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,35 @@ describe('resource embeddings', () => {
user: 'user-1234',
});
});

test('create: encoding_format=float should create float32 embeddings', async () => {
const response = await client.embeddings.create({
input: 'The quick brown fox jumped over the lazy dog',
model: 'text-embedding-3-small',
});

expect(response.data?.at(0)?.embedding).toBeInstanceOf(Array);
expect(Number.isFinite(response.data?.at(0)?.embedding.at(0))).toBe(true);
});

test('create: encoding_format=base64 should create float32 embeddings', async () => {
const response = await client.embeddings.create({
input: 'The quick brown fox jumped over the lazy dog',
model: 'text-embedding-3-small',
encoding_format: 'base64',
});

expect(response.data?.at(0)?.embedding).toBeInstanceOf(Array);
expect(Number.isFinite(response.data?.at(0)?.embedding.at(0))).toBe(true);
});

test('create: encoding_format=default should create float32 embeddings', async () => {
const response = await client.embeddings.create({
input: 'The quick brown fox jumped over the lazy dog',
model: 'text-embedding-3-small',
});

expect(response.data?.at(0)?.embedding).toBeInstanceOf(Array);
expect(Number.isFinite(response.data?.at(0)?.embedding.at(0))).toBe(true);
});
});