Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix regression in how array input values are transformed
  • Loading branch information
mattt committed May 29, 2024
commit 10146e2b2635295b7661308fc6f0cdfe2bed48b2
44 changes: 44 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,50 @@ describe("Replicate client", () => {
const collections = await client.collections.list();
expect(collections.results.length).toBe(2);
});

describe("predictions.create", () => {
test("Handles array input correctly", async () => {
const inputArray = ["Alice", "Bob", "Charlie"];

nock(BASE_URL)
.post("/predictions", {
version:
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
input: {
text: inputArray,
},
})
.reply(200, {
id: "ufawqhfynnddngldkgtslldrkq",
model: "replicate/hello-world",
version:
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
urls: {
get: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq",
cancel:
"https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel",
},
created_at: "2022-04-26T22:13:06.224088Z",
started_at: null,
completed_at: null,
status: "starting",
input: {
text: inputArray,
},
});

const response = await client.predictions.create({
version:
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
input: {
text: inputArray,
},
});

expect(response.input).toEqual({ text: inputArray });
expect(response.status).toBe("starting");
});
});
// Add more tests for error handling, edge cases, etc.
});

Expand Down
5 changes: 3 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ async function transformFileInputsToBase64EncodedDataURIs(inputs) {
// Walk a JavaScript object and transform the leaf values.
async function transform(value, mapper) {
if (Array.isArray(value)) {
let copy = [];
const copy = [];
for (const val of value) {
copy = await transform(val, mapper);
const transformed = await transform(val, mapper);
copy.push(transformed);
}
return copy;
}
Expand Down