#๐Ÿ”’ OutputParserException: This output parser can only be used with a chat generation

5 messages ยท Page 1 of 1 (latest)

floral raptor
#

My code:

class Person(BaseModel):
    name: str
    age: int

epo_id = "mistralai/Mistral-7B-Instruct-v0.1"

llm = HuggingFaceEndpoint(
    repo_id=repo_id,  
    max_new_tokens=500,
    repetition_penalty=1.15,
    do_sample=True,
    top_k=10,
    # num_return_sequences=1,
    # token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
)

passage = "Here is a passage: John, a 30-year-old software engineer, is working on a new project."
prompt_template_string = "Based on the passage, extract the name and age of the person."

prompt_template = PromptTemplate(
    template=prompt_template_string,
    input_variables=["user_question"]
)

extraction_chain = create_structured_output_runnable(output_schema=Person, llm=llm, prompt=prompt_template)
extracted_data = extraction_chain.invoke(input = {
    "input": passage
})

print(f"Extracted name: {extracted_data.name}")
print(f"Extracted age: {extracted_data.age}")

Getting this error when trying to run it:

    raise OutputParserException(
langchain_core.exceptions.OutputParserException: This output parser can only be used with a chat generation.```

So I put two print() statements in to see what was triggering the error (`print(generation)` and `print(type(generation))`):

class OutputFunctionsParser(BaseGenerationOutputParser[Any]):
...
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
generation = result[0]
print(generation)
print(type(generation))
if not isinstance(generation, ChatGeneration):
raise OutputParserException(
"This output parser can only be used with a chat generation."
)


Prints as:

text='\nA: Unnamed, 25'
<class 'langchain_core.outputs.generation.Generation'>

Why isn't the internal method returning a ChatGeneration?
swift nebulaBOT
#

Hey @floral raptor!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
swift nebulaBOT
#

@floral raptor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

swift nebulaBOT
#

@floral raptor

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.