dify icon indicating copy to clipboard operation
dify copied to clipboard

ParameterExtractorNode error

Open zxfishhack opened this issue 9 months ago • 1 comments

Self Checks

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

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Sometimes LLM response toolcall with follow arguments:

 {"digital": 3, "control_device": 0, "meeting": 0, "room_analyze": 0, "mode_event_plan_rule": 0, "browser": 0}{"digital": 6}

which will result in JSONDecodeError.

2. Additional context or comments

No response

3. Can you help us with this feature?

  • [ ] I am interested in contributing to this feature.

zxfishhack avatar Apr 09 '25 08:04 zxfishhack

To resolve the JSONDecodeError with the ParameterExtractorNode, ensure the JSON string is properly formatted. The error occurs because two JSON objects are concatenated without a separator, which is invalid JSON. You can split the string and load each JSON object separately if they are meant to be separate.

Here's a code example to handle this:

import json

def extract_tool_calls(self, llm_result_chunk: LLMResultChunk) -> list[tuple[str, str, dict[str, Any]]]:
    tool_calls = []
    for prompt_message in llm_result_chunk.delta.message.tool_calls:
        args = {}
        if prompt_message.function.arguments != "":
            # Split the string if necessary and load each JSON object
            json_strings = prompt_message.function.arguments.split('}{')
            json_strings = [s + '}' if not s.endswith('}') else s for s in json_strings]
            json_strings = ['{' + s if not s.startswith('{') else s for s in json_strings]
            
            for json_str in json_strings:
                try:
                    args.update(json.loads(json_str))
                except json.JSONDecodeError as e:
                    # Handle the error or log it
                    print(f"Error decoding JSON: {e}")

        tool_calls.append(
            (
                prompt_message.id,
                prompt_message.function.name,
                args,
            )
        )

    return tool_calls

This code splits concatenated JSON objects and loads them separately, updating the args dictionary with each valid JSON object [1]. Adjust the logic as needed based on your application's requirements.

To continue talking to Dosu, mention @dosu.


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

dosubot[bot] avatar Apr 09 '25 09:04 dosubot[bot]