> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Parsing Model Output Demo 1

> Demonstrates using output parsers to enforce LLM response formats and convert comma separated model outputs into native Python lists for reliable downstream processing.

In this lesson we explore output parsers and how they convert free-text responses from large language models into structured Python data. Output parsers make it simple to enforce predictable formats (CSV, JSON, lists) so downstream code can consume model outputs reliably.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Xqjckn2TzkOV2Gz2/images/LangChain/Interacting-with-LLMs/Parsing-Model-Output-Demo-1/jupyter-notebook-empty-code-cell.jpg?fit=max&auto=format&n=Xqjckn2TzkOV2Gz2&q=85&s=01103a5ee3ca010e10815b4051b429b3" alt="The image shows the interface of a Jupyter Notebook, displaying an empty code cell ready for input. The toolbar and menu options are visible at the top." width="1920" height="1080" data-path="images/LangChain/Interacting-with-LLMs/Parsing-Model-Output-Demo-1/jupyter-notebook-empty-code-cell.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  This lesson assumes your OpenAI API key is already configured in your environment.
</Callout>

## Quick overview

* Goal: Instruct an LLM to return a comma-separated list and parse it into a native Python `list`.
* Approach: Append parser-provided `format_instructions` to the prompt so the model emits a predictable format, then use the parser to convert the text to Python objects.

## 1. Imports and basic prompt setup

Import the required classes, create an LLM client, and define a simple prompt template.

```python theme={null}
from langchain.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain.output_parsers import CommaSeparatedListOutputParser

llm = OpenAI()
prompt = PromptTemplate(
    template="List 3 {things}",
    input_variables=["things"]
)
```

## 2. Invoke the model with a simple prompt (raw output)

Call the LLM without any parser instructions to see the default textual output.

```python theme={null}
raw = llm.invoke(input=prompt.format(things="countries that play cricket in world cup"))
print(raw)
```

Example raw output (string)

```text theme={null}
1. India
2. Australia
3. England
```

This raw response is a plain string with numbered lines. To use it programmatically you'd typically write manual parsing logic (strip numbering, split lines). Output parsers automate this process.

## 3. Add a CommaSeparatedListOutputParser and get format instructions

The `CommaSeparatedListOutputParser` provides human-readable instructions that you append to your prompt. These instructions encourage the LLM to produce CSV-style output that can be parsed reliably.

```python theme={null}
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
print(format_instructions)
```

Format instructions printed:

```text theme={null}
Your response should be a list of comma separated values, eg: `foo, bar, baz`
```

## 4. Inject the format instructions into the prompt

Use `partial_variables` in `PromptTemplate` to include the parser's `format_instructions` in the prompt. This keeps your prompt template flexible and reusable.

```python theme={null}
prompt = PromptTemplate(
    template="List 3 {things}.\n{format_instructions}",
    input_variables=["things"],
    partial_variables={"format_instructions": format_instructions}
)

final_prompt = prompt.format(things="countries that play cricket in world cup")
print(final_prompt)
```

The constructed `final_prompt` will look like:

```text theme={null}
List 3 countries that play cricket in world cup.
Your response should be a list of comma separated values, eg: `foo, bar, baz`
```

<Callout icon="lightbulb" color="#1CB2FE">
  Tip: Using `partial_variables` lets you add dynamic instructions (like format hints) without changing the main prompt template every time.
</Callout>

## 5. Invoke the model with the parser-aware prompt and parse the output

Now the model is instructed to return CSV-style text. After receiving the text, feed it to `output_parser.parse()` to get a Python list.

```python theme={null}
output = llm.invoke(input=final_prompt)
print(output.strip())       # e.g., "India, Australia, England"
print(type(output))         # <class 'str'>

things = output_parser.parse(output)
print(things)               # ['India', 'Australia', 'England']
print(type(things))         # <class 'list'>
print(things[0])            # 'India'
```

Example console outputs:

```text theme={null}
India, Australia, England
<class 'str'>
['India', 'Australia', 'England']
<class 'list'>
India
```

## Summary

* Append parser `format_instructions` to your prompt to guide the model toward a predictable output format (CSV in this example).
* Use `CommaSeparatedListOutputParser` to transform the returned string into a native Python `list`.
* This pattern reduces brittle string processing and improves data reliability when integrating LLM responses into applications.

## Quick reference table

| Component                        | Purpose                                           | Example                                                                   |
| -------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------- |
| `PromptTemplate`                 | Reusable prompt structure with variables          | `PromptTemplate(template="List 3 {things}.\n{format_instructions}", ...)` |
| `OpenAI()`                       | LLM client / runtime                              | `llm = OpenAI()`                                                          |
| `CommaSeparatedListOutputParser` | Instructs and parses CSV-like outputs into `list` | `things = output_parser.parse(output)`                                    |

## Next steps

* Try other output parsers (for example, JSON-specific parsers) if you need objects/dictionaries directly from the model.
* Combine parser format instructions with few-shot examples when you need stronger conditioning.
* Validate parsed outputs before using them in production systems to handle cases where the model doesn't follow instructions exactly.

## Links and references

* [LangChain: PromptTemplate](https://langchain.readthedocs.io/)
* [OpenAI API documentation](https://platform.openai.com/docs)

<Callout icon="warning" color="#FF6B6B">
  LLMs may sometimes ignore formatting instructions. Always validate parser output and add fallback handling for unexpected formats.
</Callout>

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/ae260750-791b-496c-991f-0d0333f61e40/lesson/1d5a8e19-7bfd-497d-8e3e-8ffd23c70eb3" />
</CardGroup>
