langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
26.94k stars 3.57k forks source link

Null Value in Tweaks Dictionary for Python Code #2615

Closed edwinjosechittilappilly closed 1 month ago

edwinjosechittilappilly commented 1 month ago

Null Value in Tweaks Dictionary for Python Code

Title: Null Value in Tweaks Dictionary for Python Code

Description: I have encountered an issue where the Tweaks Dictionary generated for Python code contains a value null instead of None. This results in an error because null is not defined in Python, and it needs to be replaced with None for the code to run correctly.

Error Message:

NameError: name 'null' is not defined

Details:

Environment:

Steps to Reproduce:

  1. Generate Tweaks for Python code.
  2. Observe the Tweaks Dictionary containing null values.
  3. Attempt to run the Python code.
  4. Encounter the NameError due to null being undefined.

Expected Behavior: The Tweaks Dictionary should contain None values instead of null to ensure compatibility with Python syntax.

Actual Behavior: The Tweaks Dictionary contains null values, causing a NameError when running the Python code.

Additional Context: This issue impacts the execution of Python code when using generated Tweaks, as the null values need to be manually replaced with None.

Screenshots: N/A

Logs:

NameError: name 'null' is not defined

Possible Solution: Update the Tweaks generation process to replace null with None for Python code.

Thank you for your assistance!

dosubot[bot] commented 1 month ago

To resolve the issue of null values in the Tweaks Dictionary for Python code, you can update the getPythonCode function to replace null with None. Here is the updated code:

/**
 * Function to get the python code for the API
 * @param {string} flow - The current flow
 * @param {any[]} tweaksBuildedObject - The tweaks
 * @returns {string} - The python code
 */
export default function getPythonCode(
  flowName: string,
  tweaksBuildedObject: any[]
): string {
  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")
      .replace(/null/g, "None");
  }

  return `from langflow.load import run_flow_from_json
TWEAKS = ${tweaksString}

result = run_flow_from_json(flow="${flowName}.json",
                            input_value="message",
                            fallback_to_env_vars=True, # False by default
                            tweaks=TWEAKS)`;
}

This modification ensures that any null values in the JSON string are replaced with None to avoid the NameError in Python [1].

To continue talking to Dosu, mention @dosu.