Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a JSON string and get an error mismatched or missing input parameter #7551

Closed
2 of 14 tasks
andrea-mucci opened this issue Jul 11, 2023 · 3 comments
Closed
2 of 14 tasks
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@andrea-mucci
Copy link

System Info

LangChain: 0.0.230
Python: 3.10

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

I have a dictionary in python

dict = {
                          "keyfeatures": [
                              {
                                  "title": "Search for Charitable Activities",
                                  "description": "The system must provide a search function that allows users to enter keywords and returns a list of charitable activities that match those keywords."
                              },
                              {
                                  "title": "Display Charitable Activities",
                                  "description": "The system must display the search results in a user-friendly format."
                              },
                              {
                                  "title": "Filter and Sort Charitable Activities",
                                  "description": "The system must provide options for users to filter and sort the search results."
                              },
                              {
                                  "title": "View Details of a Charitable Activity",
                                  "description": "The system must allow users to select a charitable activity from the search results and view more detailed information about it."
                              },
                              {
                                  "title": "Save or Bookmark Charitable Activities",
                                  "description": "The system must allow users to save or bookmark charitable activities that they're interested in."
                              }
                          ]
                      }

i convert the dictionary in a json string

json_string = json.dumps(dict)

and i obtain the following string

{"keyfeatures": [{"title": "Search for Charitable Activities", "description": "The system must provide a search function that allows users to enter keywords and returns a list of charitable activities that match those keywords."}, {"title": "Display Charitable Activities", "description": "The system must display the search results in a user-friendly format."}, {"title": "Filter and Sort Charitable Activities", "description": "The system must provide options for users to filter and sort the search results."}, {"title": "View Details of a Charitable Activity", "description": "The system must allow users to select a charitable activity from the search results and view more detailed information about it."}, {"title": "Save or Bookmark Charitable Activities", "description": "The system must allow users to save or bookmark charitable activities that they're interested in."}]}

if i pass that string to an AIMessagePromptTemplate

AIMessagePromptTemplate.from_template(msg)

i get the following error:


File "/home/andrea/PycharmProjects/ArchAI/venv/lib/python3.10/site-packages/langchain/prompts/chat.py", line 85, in from_template
    prompt = PromptTemplate.from_template(template, template_format=template_format)
  File "/home/andrea/PycharmProjects/ArchAI/venv/lib/python3.10/site-packages/langchain/prompts/prompt.py", line 145, in from_template
    return cls(
  File "/home/andrea/PycharmProjects/ArchAI/venv/lib/python3.10/site-packages/langchain/load/serializable.py", line 74, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for PromptTemplate
__root__
  Invalid prompt schema; check for mismatched or missing input parameters. '"title"' (type=value_error)

i have already added f-string formater with multiple curly-brackets but still fail

thanks in advance

Expected behavior

the PromptMessage must import a formatted JSON string correctly without interfere with internal template curly-brackets format

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jul 11, 2023
@dosubot
Copy link

dosubot bot commented Jul 11, 2023

Answer generated by a 🤖

Answer

I understand that you're trying to pass a JSON string to the AIMessagePromptTemplate.from_template(msg) method and encountering an error about mismatched or missing input parameters. This issue arises because the method expects a template string with placeholders for variables, not a JSON string. The placeholders in the template string should be in the format {variable_name}.

When you pass a JSON string to this method, it attempts to interpret the keys in your JSON string ("title", "description", etc.) as variable names in a template string. This is why you're seeing the error message about an invalid prompt schema and mismatched or missing input parameters.

To resolve this issue, you need to convert your JSON data into a suitable template string format before passing it to the AIMessagePromptTemplate.from_template(msg) method. For instance, if you want to create a prompt template that includes the title and description of each feature, you could do something like this:

template_string = '\n'.join([f"{item['title']}: {item['description']}" for item in dict['keyfeatures']])
prompt_template = AIMessagePromptTemplate.from_template(template_string)

This code will create a template string where each line is a title and description from your JSON data, and then create a prompt template from that string.

Please note that this is one potential solution based on the information provided. If you believe this is a bug that could impact other users, feel free to make a pull request with a proposed fix. If this solution doesn't align with your experience, I recommend upgrading to the latest version of LangChain to ensure you have the most recent features and bug fixes.

Sources


This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

@andrea-mucci
Copy link
Author

The answer is not totally correct

imagine that i have a prompt where i would represent as an example a prompt question and an answer that is a json
to pass that json string I need to put a JSON string on that prompt.

The question is:
How i would put a JSON string into a prompt?

Thanks in advance

@andrea-mucci
Copy link
Author

I found a solution:

just use jinja as template format

AIMessagePromptTemplate.from_template(msg, template_format="jinja2")

and the prompt variables must use {{variable}} and not {variable}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant