langchain-ai / langchain

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

Received JSONDecodeError Expecting property name enclosed in double quotes #19699

Closed nadworny closed 1 month ago

nadworny commented 7 months ago

Checked other resources

Example Code

the following notebook doesn't work for me: https://github.com/langchain-ai/rag-from-scratch/blob/main/rag_from_scratch_10_and_11.ipynb

when executing this code:

question = """Why doesn't the following code work:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(["human", "speak in {language}"])
prompt.invoke("french")
"""

result = router.invoke({"question": question})

Error Message and Stack Trace (if applicable)


Cell In[3], line 9
      1 question = """Why doesn't the following code work:
      2 
      3 from langchain_core.prompts import ChatPromptTemplate
   (...)
      6 prompt.invoke("french")
      7 """
----> 9 result = router.invoke({"question": question})

File ~/workspace/prototyping/rag/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py:2415, in RunnableSequence.invoke(self, input, config)
   2413 try:
   2414     for i, step in enumerate(self.steps):
-> 2415         input = step.invoke(
   2416             input,
   2417             # mark each step as a child run
   2418             patch_config(
   2419                 config, callbacks=run_manager.get_child(f"seq:step:{i+1}")
   2420             ),
   2421         )
   2422 # finish the root run
   2423 except BaseException as e:

File ~/workspace/prototyping/rag/.venv/lib/python3.11/site-packages/langchain_core/output_parsers/base.py:169, in BaseOutputParser.invoke(self, input, config)
...
{
  datasource: "python_docs"
}

are not valid JSON. Received JSONDecodeError Expecting property name enclosed in double quotes: line 2 column 3 (char 4)

Description

Using the following structured output code:

class RouteQuery(BaseModel):
    """Route a user query to the most relevant datasource."""

    datasource: Literal["python_docs", "js_docs", "golang_docs"] = Field(
        ...,
        description="Given a user question choose which datasource would be most relevant for answering their question",
    )

# LLM with function call 
structured_llm = llm.with_structured_output(RouteQuery)

I'm using AzureChatOpenAI as LLM.

System Info

langchain==0.0.352 langchain-community==0.0.5 langchain-core==0.1.2

pyproject deps: python = "^3.11" python-dotenv = "^1.0.1" langchain-community = "^0.0.29" tiktoken = "^0.6.0" langchain-openai = "^0.1.1" langchainhub = "^0.1.15" chromadb = "^0.4.24" langchain = "^0.1.13" youtube-transcript-api = "^0.6.2" pytube = "^15.0.0" httpx = "^0.27.0" h11 = "^0.14.0" distro = "^1.9.0" pydantic = ">2"

liugddx commented 7 months ago

this works for me

nadworny commented 7 months ago

this works for me

@liugddx which GPT model do you use? What are your python packages versions?

nicolasang333 commented 7 months ago

I am facing the same problem. Following the guide (https://python.langchain.com/docs/use_cases/extraction/how_to/examples)

class Person(BaseModel):
    # ^ Doc-string for the entity Person.
    # This doc-string is sent to the LLM as the description of the schema Person,
    # and it can help to improve extraction results.

    # Note that:
    # 1. Each field is an `optional` -- this allows the model to decline to extract it!
    # 2. Each field has a `description` -- this description is used by the LLM.
    # Having a good description can help improve extraction results.

    name: Optional[str] = Field(..., description="The name of the person")
    hair_color: Optional[str] = Field(
        ..., description="The color of the peron's eyes if known"
    )
    height_in_meters: Optional[str] = Field(..., description="Height in METERs")

Using ChatOpenAI

runnable = prompt | personal.with_structured_output(
    schema=Person,
    method="function_calling",
    include_raw=False)

Using AzureChatOpenAI

runnable_azurechat_openai = prompt | azure_chat.with_structured_output(
    schema=Person,
    method="function_calling",
    include_raw=False,
)

image

Error Message for AzureChatOpenAI


    "name": "OutputParserException",
    "message": "Function Person arguments:\n\n({\n  name: \"Harrison\",\n  hair_color: \"black\",\n  height_in_meters: null\n})\n\nare not valid JSON. Received JSONDecodeError Expecting value: line 1 column 1 (char 0)",```
vanhauknc commented 7 months ago

I facing with same problem

pratikkotian04 commented 6 months ago

I am also facing the same problem with AzureOpenAI

bhs1 commented 4 months ago

AlphaCodium paper shows that using yaml instead of json for code generation is better. Would be great if someone updated this library to use yaml. Any known issues with doing that? See screenshot below: Screenshot 2024-06-17 at 11 36 45 AM