langchain: Invalid Output Parser Format for "Router Chain"

System Info

langchain version: 0.0.170 python: 3.8

Who can help?

No response

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Here I came across an issue related to the output of router chain. When I ran the tutorial of “router chain” in langchain website, the input query is: “What is black body radiation?” and the output of LLM is:

'{
    "destination": "physics",
    "next_inputs": "What is black body radiation?"
}'

Use the class RouterOutputParser to parse the output then I got the error:

{OutputParserException}Got invalid return object. Expected markdown code snippet with JSON object, but got: { “destination”: “physics”, “next_inputs”: “What is black body radiation?” }

When I debug step by step I found the error raised in this function: parse_json_markdown

def parse_json_markdown(text: str, expected_keys: List[str]) -> Any:
    if "```json" not in text:
        raise OutputParserException(
            f"Got invalid return object. Expected markdown code snippet with JSON "
            f"object, but got:\n{text}"
        )

    json_string = text.split("```json")[1].strip().strip("```").strip()
    try:
        json_obj = json.loads(json_string)
    except json.JSONDecodeError as e:
        raise OutputParserException(f"Got invalid JSON object. Error: {e}")
    for key in expected_keys:
        if key not in json_obj:
            raise OutputParserException(
                f"Got invalid return object. Expected key `{key}` "
                f"to be present, but got {json_obj}"
            )
    return json_obj

You can see there is no “```json” string in the output of LLM, so it will step into the “if” in the first row of this function and raise the bug.

Expected behavior

Can anyone give me some solutions? thanks.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 4
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I resolved the problem by adding an example at the end of the MULTI_PROMPT_ROUTER_TEMPLATE. Please check it as below.

eg:

<< INPUT >> “What is black body radiation?” << OUTPUT >> ```json {{{{ “destination”: “physics”, “next_inputs”: “What is black body radiation?” }}}} ```

I resolved the problem by adding an example at the end of the MULTI_PROMPT_ROUTER_TEMPLATE. Please check it as below.

eg:

<< INPUT >> “What is black body radiation?” << OUTPUT >> json {{{{ "destination": "physics", "next_inputs": "What is black body radiation?" }}}}

In addition, to suggested solution, found out that the following worked just as well.

<< OUTPUT (must include ```json at the start of the response) >>

It fixed in version langchain 0.0.222. Just upgraded langchain and it work pip install --upgrade langchain

image

I resolved the problem by adding an example at the end of the MULTI_PROMPT_ROUTER_TEMPLATE. Please check it as below.

eg:

<< INPUT >> “What is black body radiation?” << OUTPUT >> json {{{{ "destination": "physics", "next_inputs": "What is black body radiation?" }}}}

In addition, to suggested solution, found out that the following worked just as well.

<< OUTPUT (must include ```json at the start of the response) >>

Combining the above answer and the answer from @lucarp, helped in my case. Updating the << OUTPUT >> on the MULTI_PROMPT_ROUTER_TEMPLATE as below worked:

<< OUTPUT (must include ```json at the start of the response and must end with ```) >>