langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
91.48k stars 14.57k forks source link

[OutputFixingParser] I am using the OutputFixingParser component according to the official documentation, but an exception has occurred #24219

Open ZhangShenao opened 1 month ago

ZhangShenao commented 1 month ago

Checked other resources

Example Code

The following code:

import os
from typing import List

import dotenv
from langchain.output_parsers import OutputFixingParser
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI

dotenv.load_dotenv()

class Actor(BaseModel):
    name: str = Field(description="name of an actor")
    film_names: List[str] = Field(description="list of names of films they starred in")

actor_query = "Generate the filmography for a random actor."

parser = PydanticOutputParser(pydantic_object=Actor)

misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"

new_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI(openai_api_base=os.getenv('OPENAI_API_BASE')))

print(new_parser.parse(misformatted))

Error Message and Stack Trace (if applicable)

Traceback (most recent call last): File "/Users/zhangshenao/Desktop/LLM/happy-langchain/6-输出解析/2.使用OutputFixingParser自动修复解析器.py", line 39, in print(new_parser.parse(misformatted)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/output_parsers/fix.py", line 74, in parse completion = self.retry_chain.invoke( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 2497, in invoke input = step.invoke(input, config, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/prompts/base.py", line 179, in invoke return self._call_with_config( ^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1593, in _call_with_config context.run( File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 380, in call_func_with_variable_args return func(input, kwargs) # type: ignore[call-arg] ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/prompts/base.py", line 153, in _format_prompt_with_error_handling _inner_input = self._validate_input(inner_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain_core/prompts/base.py", line 145, in _validate_input raise KeyError( KeyError: "Input to PromptTemplate is missing variables {'completion'}. Expected: ['completion', 'error', 'instructions'] Received: ['instructions', 'input', 'error']"

Description

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101 Python Version: 3.12.3 (v3.12.3:f6650f9ad7, Apr 9 2024, 08:18:47) [Clang 13.0.0 (clang-1300.0.29.30)]

Package Information

langchain_core: 0.2.12 langchain: 0.2.7 langchain_community: 0.2.7 langsmith: 0.1.82 langchain_huggingface: 0.0.3 langchain_openai: 0.1.14 langchain_text_splitters: 0.2.2

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph langserve

rahil278 commented 1 month ago

I have been using the same parser. After updating the langchain and langchain_community from 0.2.5 to 0.2.7 this bug occurs. For immediate fix I think reverting to 0.2.5 should solve it

eyurtsev commented 1 month ago

Should be fixed by this: https://github.com/langchain-ai/langchain/pull/23967/files

Stefan-Chisl commented 1 week ago

I could reproduce this error with langchain-core==0.2.33.

KeyError: "Input to PromptTemplate is missing variables {'completion'}.  Expected: ['completion', 'error', 'instructions'] Received: ['instructions', 'input', 'error']"

when called with chat model.

Similar: #24105