Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JSON string when using Array[String] in HTTP request node #12759

Open
5 tasks done
bowenliang123 opened this issue Jan 15, 2025 · 3 comments · May be fixed by #12761
Open
5 tasks done

Invalid JSON string when using Array[String] in HTTP request node #12759

bowenliang123 opened this issue Jan 15, 2025 · 3 comments · May be fixed by #12761
Labels
🐞 bug Something isn't working

Comments

@bowenliang123
Copy link
Contributor

bowenliang123 commented Jan 15, 2025

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.15.1

Cloud or Self Hosted

Self Hosted (Docker), Self Hosted (Source)

Steps to reproduce

  1. Generate an string array from Python Executor
def main() -> dict:
    return {
        "result": ["a","b"],
    }
  1. Use the string array variable in HTTP Request node
image image

✔️ Expected Behavior

No response

❌ Actual Behavior

image

No response

@dosubot dosubot bot added 🐞 bug Something isn't working labels Jan 15, 2025
Copy link

dosubot bot commented Jan 15, 2025

I found a similar unsolved discussion titled JSON not recognizing a string item from an iteration, which might be related to the issue you're experiencing [1].

To address the JSON parsing error when using a string array in the HTTP Request node in Dify version 0.15.1, ensure that the JSON string is correctly formatted before it is parsed. The _init_body method in the Executor class attempts to parse the JSON string using json.loads(json_string, strict=False). If the JSON string is not correctly formatted, a json.JSONDecodeError will be raised.

Here's a snippet of the relevant code:

case "json":
    if len(data) != 1:
        raise RequestBodyError("json body type should have exactly one item")
    json_string = self.variable_pool.convert_template(data[0].value).text
    try:
        json_object = json.loads(json_string, strict=False)
    except json.JSONDecodeError as e:
        raise RequestBodyError(f"Failed to parse JSON: {json_string}") from e
    self.json = json_object

Ensure that the JSON string is valid and properly formatted before it is passed to the HTTP Request node. If the JSON string is generated dynamically, verify that all necessary escaping and formatting are correctly applied. If the error persists, inspect the JSON string being passed to identify any syntax issues [2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@bowenliang123
Copy link
Contributor Author

Your suggested case is not using any of string array instance.
Furthermore, we should never pervent segement usage of virables in HTTP node's JSON body, by forcing it pre-generated.

@mrkenknight
Copy link

/api/core/workflow/nodes/http_request/executor.py
Change line:
json_object = json.loads(json_string, strict=False)

To:
json_object = json.loads(json_string.replace("'", '"'), strict=False)

This work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
2 participants