MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / runPythonCode

Function runPythonCode

src/python/wrapper.ts:16–40  ·  view source on GitHub ↗
(
  code: string,
  method: string,
  args: (string | object | undefined)[],
)

Source from the content-addressed store, hash-verified

14 * @returns {Promise<T>} - The result from executing the Python code.
15 */
16export async function runPythonCode<T = unknown>(
17 code: string,
18 method: string,
19 args: (string | object | undefined)[],
20): Promise<T> {
21 let tempDirectory: string | undefined;
22 try {
23 tempDirectory = await createSecureTempDirectory('promptfoo-python-code-');
24 const tempFilePath = await writeSecureTempFile(tempDirectory, 'script.py', code);
25 // Necessary to await so temp file doesn't get deleted.
26 const result = await runPython<T>(tempFilePath, method, args);
27 return result;
28 } catch (error) {
29 logger.error(`Error executing Python code: ${error}`);
30 throw error;
31 } finally {
32 if (tempDirectory) {
33 try {
34 await removeSecureTempDirectory(tempDirectory);
35 } catch (error) {
36 logger.error(`Error removing temporary Python code directory: ${error}`);
37 }
38 }
39 }
40}

Callers 2

wrapper.test.tsFile · 0.90
handlePythonFunction · 0.90

Calls 4

writeSecureTempFileFunction · 0.90
errorMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…