diff --git a/js/src/client.ts b/js/src/client.ts index 23f33d515..b3f0a1e03 100644 --- a/js/src/client.ts +++ b/js/src/client.ts @@ -2735,7 +2735,7 @@ export class Client implements LangSmithTracingClientInterface { // add attachments back to the example example.attachments = Object.entries(attachment_urls).reduce( (acc, [key, value]) => { - acc[key] = { + acc[key.slice("attachment.".length)] = { presigned_url: value.presigned_url, }; return acc; @@ -2832,7 +2832,7 @@ export class Client implements LangSmithTracingClientInterface { if (attachment_urls) { example.attachments = Object.entries(attachment_urls).reduce( (acc, [key, value]) => { - acc[key] = { + acc[key.slice("attachment.".length)] = { presigned_url: value.presigned_url, }; return acc; diff --git a/js/src/tests/client.int.test.ts b/js/src/tests/client.int.test.ts index 18582a01b..de8710e1f 100644 --- a/js/src/tests/client.int.test.ts +++ b/js/src/tests/client.int.test.ts @@ -1410,24 +1410,24 @@ test("update examples multipart", async () => { let updatedExample = await client.readExample(exampleId); expect(updatedExample.inputs.text).toEqual("hello world2"); expect(Object.keys(updatedExample.attachments ?? {}).sort()).toEqual( - ["attachment.bar", "attachment.test_file"].sort() + ["bar", "test_file"].sort() ); expect(updatedExample.metadata).toEqual({ bar: "foo" }); let attachmentData: Uint8Array | undefined = updatedExample.attachments?.[ - "attachment.test_file" + "test_file" ].presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file"].presigned_url + updatedExample.attachments?.["test_file"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; expect(attachmentData).toEqual(new Uint8Array(fs.readFileSync(pathname))); - attachmentData = updatedExample.attachments?.["attachment.bar"].presigned_url + attachmentData = updatedExample.attachments?.["bar"].presigned_url ? new Uint8Array( - (await fetch( - updatedExample.attachments?.["attachment.bar"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(updatedExample.attachments?.["bar"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; expect(attachmentData).toEqual(new Uint8Array(fs.readFileSync(pathname))); @@ -1443,14 +1443,11 @@ test("update examples multipart", async () => { await client.updateExamplesMultipart(dataset.id, [exampleUpdate4]); updatedExample = await client.readExample(exampleId); expect(updatedExample.metadata).toEqual({ foo: "bar" }); - expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "attachment.test_file2", - ]); - attachmentData = updatedExample.attachments?.["attachment.test_file2"] - .presigned_url + expect(Object.keys(updatedExample.attachments ?? {})).toEqual(["test_file2"]); + attachmentData = updatedExample.attachments?.["test_file2"].presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file2"].presigned_url + updatedExample.attachments?.["test_file2"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -1471,14 +1468,11 @@ test("update examples multipart", async () => { foo: "bar", dataset_split: ["foo", "bar"], }); - expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "attachment.test_file", - ]); - attachmentData = updatedExample.attachments?.["attachment.test_file"] - .presigned_url + expect(Object.keys(updatedExample.attachments ?? {})).toEqual(["test_file"]); + attachmentData = updatedExample.attachments?.["test_file"].presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file"].presigned_url + updatedExample.attachments?.["test_file"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; diff --git a/js/src/tests/evaluate_attachments.int.test.ts b/js/src/tests/evaluate_attachments.int.test.ts index 9a78a3f9a..0a64a54f9 100644 --- a/js/src/tests/evaluate_attachments.int.test.ts +++ b/js/src/tests/evaluate_attachments.int.test.ts @@ -34,19 +34,19 @@ test("evaluate can handle examples with attachments", async () => { config?: TargetConfigT ) => { // Verify we receive the attachment data - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["attachment.image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -57,16 +57,15 @@ test("evaluate can handle examples with attachments", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -135,16 +134,15 @@ test("evaluate with attachments not in target function", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -212,19 +210,19 @@ test("multiple evaluators with attachments", async () => { config?: TargetConfigT ) => { // Verify we receive the attachment data - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["attachment.image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -235,16 +233,15 @@ test("multiple evaluators with attachments", async () => { const customEvaluatorOne = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -259,16 +256,15 @@ test("multiple evaluators with attachments", async () => { const customEvaluatorTwo = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -333,19 +329,19 @@ test("evaluate with attachments runnable target function", async () => { await client.uploadExamplesMultipart(dataset.id, [example]); const myFunction = async (_input: any, config?: any) => { - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["attachment.image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -359,16 +355,15 @@ test("evaluate with attachments runnable target function", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; diff --git a/python/langsmith/client.py b/python/langsmith/client.py index a92a89659..f683a36f1 100644 --- a/python/langsmith/client.py +++ b/python/langsmith/client.py @@ -3900,7 +3900,7 @@ def read_example( response = requests.get(value["presigned_url"], stream=True) response.raise_for_status() reader = io.BytesIO(response.content) - attachments[key.split(".")[1]] = { + attachments[key.removeprefix("attachment.")] = { "presigned_url": value["presigned_url"], "reader": reader, } @@ -3986,7 +3986,7 @@ def list_examples( response = requests.get(value["presigned_url"], stream=True) response.raise_for_status() reader = io.BytesIO(response.content) - attachments[key.split(".")[1]] = { + attachments[key.removeprefix("attachment.")] = { "presigned_url": value["presigned_url"], "reader": reader, }