langchain-ai / langchain

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

Error on guide : "How to parse JSON output" #26523

Closed demangejeremy closed 1 week ago

demangejeremy commented 1 week ago

Checked other resources

Example Code

Here is the code:

from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from pydantic import BaseModel, Field

model = ChatOpenAI(temperature=0)

# Define your desired data structure.
class Joke(BaseModel):
    setup: str = Field(description="question to set up a joke")
    punchline: str = Field(description="answer to resolve the joke")

# And a query intented to prompt a language model to populate the data structure.
joke_query = "Tell me a joke."

# Set up a parser + inject instructions into the prompt template.
parser = JsonOutputParser(pydantic_object=Joke)

prompt = PromptTemplate(
    template="Answer the user query.\n{format_instructions}\n{query}\n",
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)

chain = prompt | model | parser

chain.invoke({"query": joke_query})

Error Message and Stack Trace (if applicable)

Here's the error:

AttributeError: 'FieldInfo' object has no attribute 'generate'

Description

The code in this guide does not work: https://python.langchain.com/docs/how_to/output_parser_json/

System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 23.5.0: Wed May  1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030
> Python Version:  3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ]

Package Information
-------------------
> langchain_core: 0.3.0
> langchain: 0.3.0
> langchain_community: 0.2.6
> langsmith: 0.1.120
> langchain_chroma: 0.1.3
> langchain_mistralai: 0.2.0
> langchain_ollama: 0.1.3
> langchain_openai: 0.2.0
> langchain_text_splitters: 0.3.0

Optional packages not installed
-------------------------------
> langgraph
> langserve

Other Dependencies
------------------
> aiohttp: 3.9.5
> async-timeout: Installed. No version info available.
> chromadb: 0.5.3
> dataclasses-json: 0.6.7
> fastapi: 0.111.1
> httpx: 0.27.0
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> numpy: 1.26.4
> ollama: 0.3.2
> openai: 1.44.0
> orjson: 3.10.6
> packaging: 23.2
> pydantic: 2.8.2
> PyYAML: 6.0.1
> requests: 2.32.2
> SQLAlchemy: 2.0.31
> tenacity: 8.5.0
> tiktoken: 0.7.0
> tokenizers: 0.19.1
> typing-extensions: 4.12.2
demangejeremy commented 1 week ago

Problem due to langchain_ollama package. I close this topic.