Closed kushagr96 closed 5 days ago
@kushagr96 Hello! I'm a bot here to help you with bug fixes, answer your questions, and guide you on becoming a contributor. If you need assistance, feel free to ask!
To resolve the ValueError: unmatched '{' in format spec
error in your RAG application, ensure that any curly brackets in the format instructions are properly escaped. The provided code already includes a mechanism to escape curly brackets in the format
method of the LangchainOutputParser
class. Here is the relevant part of the code:
def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
format_instructions = self._output_parser.get_format_instructions()
# TODO: this is a temporary hack. if there's curly brackets in the format
# instructions (and query is a string template), we need to
# escape the curly brackets in the format instructions to preserve the
# overall template.
query_tmpl_vars = {
v for _, v, _, _ in Formatter().parse(query) if v is not None
}
if len(query_tmpl_vars) > 0:
format_instructions = format_instructions.replace("{", "{{")
format_instructions = format_instructions.replace("}", "}}")
if self._format_key is not None:
fmt_query = query.format(**{self._format_key: format_instructions})
else:
fmt_query = query + "\n\n" + format_instructions
return fmt_query
This code checks if there are any template variables in the query
string and escapes the curly brackets in the format_instructions
to prevent the ValueError
. Ensure that this logic is correctly implemented and that the format
method is being used properly in your application. The LangchainOutputParser
class is designed to handle this specific issue, so make sure you are utilizing this class and its format
method as shown [1][2].
To continue talking to Dosu, mention @dosu.
Thanks, that worked!
Question Validation
Question
I am trying to use Structured output for my RAG application, but getting
ValueError: unmatched '{' in format spec
error.Here's the code for generating structured output:
This generates the following error:
llama-index versions: