diff --git a/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx b/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx index e7999b69b..9aa946746 100644 --- a/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx @@ -2,19 +2,27 @@ * Function to get the python code for the API * @param {string} flowId - The id of the flow * @param {boolean} isAuth - If the API is authenticated - * @param {any[]} tweak - The tweaks + * @param {any[]} tweaksBuildedObject - The tweaks + * @param {string} [endpointName] - The optional endpoint name * @returns {string} - The python code */ export default function getPythonApiCode( flowId: string, isAuth: boolean, - tweaksBuildedObject, - endpointName?: string + tweaksBuildedObject: any[], + endpointName?: string, ): string { - const tweaksObject = tweaksBuildedObject[0]; - const tweaksString = JSON.stringify(tweaksObject, null, 2) - .replace(/true/g, "True") - .replace(/false/g, "False"); + let tweaksString = "{}"; + if (tweaksBuildedObject && tweaksBuildedObject.length > 0) { + const tweaksObject = tweaksBuildedObject[0]; + if (!tweaksObject) { + throw new Error("expected tweaks"); + } + tweaksString = JSON.stringify(tweaksObject, null, 2) + .replace(/true/g, "True") + .replace(/false/g, "False"); + } + return `import argparse import json from argparse import RawTextHelpFormatter diff --git a/src/frontend/src/modals/apiModal/utils/get-python-code.tsx b/src/frontend/src/modals/apiModal/utils/get-python-code.tsx index 9fd8048f2..734e16bbe 100644 --- a/src/frontend/src/modals/apiModal/utils/get-python-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-python-code.tsx @@ -1,17 +1,26 @@ /** * Function to get the python code for the API * @param {string} flow - The current flow - * @param {any[]} tweak - The tweaks + * @param {any[]} tweaksBuildedObject - The tweaks * @returns {string} - The python code */ export default function getPythonCode( flowName: string, - tweaksBuildedObject + tweaksBuildedObject: any[], ): string { - const tweaksObject = tweaksBuildedObject[0]; + let tweaksString = "{}"; + if (tweaksBuildedObject && tweaksBuildedObject.length > 0) { + const tweaksObject = tweaksBuildedObject[0]; + if (!tweaksObject) { + throw new Error("expected tweaks"); + } + tweaksString = JSON.stringify(tweaksObject, null, 2) + .replace(/true/g, "True") + .replace(/false/g, "False"); + } return `from langflow.load import run_flow_from_json -TWEAKS = ${JSON.stringify(tweaksObject, null, 2)} +TWEAKS = ${tweaksString} result = run_flow_from_json(flow="${flowName}.json", input_value="message",