Skip to content

Commit 10146e2

Browse files
committed
Fix regression in how array input values are transformed
1 parent e059286 commit 10146e2

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

index.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,50 @@ describe("Replicate client", () => {
114114
const collections = await client.collections.list();
115115
expect(collections.results.length).toBe(2);
116116
});
117+
118+
describe("predictions.create", () => {
119+
test("Handles array input correctly", async () => {
120+
const inputArray = ["Alice", "Bob", "Charlie"];
121+
122+
nock(BASE_URL)
123+
.post("/predictions", {
124+
version:
125+
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
126+
input: {
127+
text: inputArray,
128+
},
129+
})
130+
.reply(200, {
131+
id: "ufawqhfynnddngldkgtslldrkq",
132+
model: "replicate/hello-world",
133+
version:
134+
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
135+
urls: {
136+
get: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq",
137+
cancel:
138+
"https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel",
139+
},
140+
created_at: "2022-04-26T22:13:06.224088Z",
141+
started_at: null,
142+
completed_at: null,
143+
status: "starting",
144+
input: {
145+
text: inputArray,
146+
},
147+
});
148+
149+
const response = await client.predictions.create({
150+
version:
151+
"5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
152+
input: {
153+
text: inputArray,
154+
},
155+
});
156+
157+
expect(response.input).toEqual({ text: inputArray });
158+
expect(response.status).toBe("starting");
159+
});
160+
});
117161
// Add more tests for error handling, edge cases, etc.
118162
});
119163

lib/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,10 @@ async function transformFileInputsToBase64EncodedDataURIs(inputs) {
310310
// Walk a JavaScript object and transform the leaf values.
311311
async function transform(value, mapper) {
312312
if (Array.isArray(value)) {
313-
let copy = [];
313+
const copy = [];
314314
for (const val of value) {
315-
copy = await transform(val, mapper);
315+
const transformed = await transform(val, mapper);
316+
copy.push(transformed);
316317
}
317318
return copy;
318319
}

0 commit comments

Comments
 (0)