KeyError when formatting FewShotPromptTemplate
Labels: name: auto:bug

### System Info

LangChain: 0.0.221
Python: 3.9.17.
OS: MacOS Ventura 13.3.1 (a) 

### Who can help?

_No response_

### Information

- [ ] The official example notebooks/scripts
- [X] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [X] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

Below is the code to reproduce the error. I attempt to set up a simple few-shot prompting logic with an LLM for named entity extraction from documents. At the moment, I supply one single document with the expected entities to be extracted, and the expected answer in key-value pairs. Then I want to format the few-shot prompt object before seeding it as an input to one of the LLM chains, and this is the step where I encounter the error:

```
KeyError                                  Traceback (most recent call last)
Cell In[1], line 56
     53 new_doc = "Kate McConnell is 56 and is soon retired according to the current laws of the state of Alaska in which she resides."
     54 new_entities_to_extract = ['name', 'organization']
---> 56 prompt = few_shot_prompt_template.format(
     57     input_doc=new_doc,
     58     entities_to_extract=new_entities_to_extract,
     59     answer={}
     60 )

File [~/Desktop/automarkup/.venv/lib/python3.9/site-packages/langchain/prompts/few_shot.py:123](https://file+.vscode-resource.vscode-cdn.net/Users/username/Desktop/automarkup/src/~/Desktop/automarkup/.venv/lib/python3.9/site-packages/langchain/prompts/few_shot.py:123), in FewShotPromptTemplate.format(self, **kwargs)
    120 template = self.example_separator.join([piece for piece in pieces if piece])
    122 # Format the template with the input variables.
--> 123 return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)

File [/opt/homebrew/Cellar/python](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Cellar/python)@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py:161, in Formatter.format(self, format_string, *args, **kwargs)
    160 def format(self, format_string, [/](https://file+.vscode-resource.vscode-cdn.net/), *args, **kwargs):
--> 161     return self.vformat(format_string, args, kwargs)

File [~/Desktop/automarkup/.venv/lib/python3.9/site-packages/langchain/formatting.py:29](https://file+.vscode-resource.vscode-cdn.net/Users/username/Desktop/automarkup/src/~/Desktop/automarkup/.venv/lib/python3.9/site-packages/langchain/formatting.py:29), in StrictFormatter.vformat(self, format_string, args, kwargs)
     24 if len(args) > 0:
     25     raise ValueError(
     26         "No arguments should be provided, "
     27         "everything should be passed as keyword arguments."
     28     )
---> 29 return super().vformat(format_string, args, kwargs)

File [/opt/homebrew/Cellar/python](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Cellar/python)@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py:165, in Formatter.vformat(self, format_string, args, kwargs)
    163 def vformat(self, format_string, args, kwargs):
    164     used_args = set()
--> 165     result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
    166     self.check_unused_args(used_args, args, kwargs)
    167     return result

File [/opt/homebrew/Cellar/python](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Cellar/python)@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py:205, in Formatter._vformat(self, format_string, args, kwargs, used_args, recursion_depth, auto_arg_index)
    201     auto_arg_index = False
    203 # given the field_name, find the object it references
    204 #  and the argument it came from
--> 205 obj, arg_used = self.get_field(field_name, args, kwargs)
    206 used_args.add(arg_used)
    208 # do any conversion on the resulting object

File [/opt/homebrew/Cellar/python](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Cellar/python)@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py:270, in Formatter.get_field(self, field_name, args, kwargs)
    267 def get_field(self, field_name, args, kwargs):
    268     first, rest = _string.formatter_field_name_split(field_name)
--> 270     obj = self.get_value(first, args, kwargs)
    272     # loop through the rest of the field_name, doing
    273     #  getattr or getitem as needed
    274     for is_attr, i in rest:

File [/opt/homebrew/Cellar/python](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Cellar/python)@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py:227, in Formatter.get_value(self, key, args, kwargs)
    225     return args[key]
    226 else:
--> 227     return kwargs[key]

KeyError: "'name'"
```

And the code is:

```
from langchain import FewShotPromptTemplate, PromptTemplate


example = {
    "input_doc": "John Doe recently celebrated his birthday, on 2023-06-30, turning 30. He has been working with us at ABC Corp for a very long time already.",
    "entities_to_extract": ["name", "date", "organization"],
    "answer": {
        "name": "John Doe",
        "date": "2023-06-30",
        "organization": "ABC Corp"
    }
}

example_template = """
User: Retrieve the following entities from the document:
Document: {input_doc}
Entities: {entities_to_extract}

AI: The extracted entities are: {answer}
"""


example_prompt = PromptTemplate(
    input_variables=["input_doc", "entities_to_extract", "answer"],
    template=example_template
)



prefix = """You are a capable large language model. Your task is to extract entities from a given document. 
Please pay attention to all the words in the following example and retrieve the requested entities.
"""

suffix = """
User: Retrieve the requested entities from the document.
Document: {input_doc}
Entities: {entities_to_extract}

AI:
"""


few_shot_prompt_template = FewShotPromptTemplate(
    examples=[example],
    example_prompt=example_prompt,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input_doc", "entities_to_extract"],
    example_separator="\n\n"
)


new_doc = "Kate McConnell is 56 and is soon retired according to the current laws of the state of Alaska in which she resides."
new_entities_to_extract = ['name', 'organization']

prompt = few_shot_prompt_template.format(
    input_doc=new_doc,
    entities_to_extract=new_entities_to_extract,
    answer={}
)
```



### Expected behavior

The expected behavior is to return the formatted template in string format, instead of the error showing up.