What is StrOutputParser?
StrOutputParser is the simplest LangChain output parser. It takes an AIMessage (the output of a chat model) or a string (from a completion model) and returns the text content as a plain Python string. It is most commonly used as the final step in an LCEL chain: chain = prompt | model | StrOutputParser(). Without StrOutputParser, invoking a chat model returns an AIMessage object—you would need to access .content manually.
StrOutputParser is stateless and has no configuration parameters. It simply calls .content if it receives a message object, or passes the string through unchanged. This makes it the right choice when you need plain text for downstream processing—logging, template rendering, further string manipulation—and you don't need structured data.
For streaming chains, StrOutputParser is stream-transparent: chain.stream(input) yields string tokens as they arrive from the model, enabling real-time typewriter-style UIs without any additional configuration. If you need structured output (JSON, Pydantic models, lists), use JsonOutputParser, PydanticOutputParser, or model.with_structured_output() instead.
When to Use
You just need the text response. Use StrOutputParser at the end of chains for simple text output.
Use Cases
- • Extract text from responses
- • Simple chatbot output
- • Text generation
- • Basic chains
- • Content extraction
- • Simple pipelines
Key Features
- ✓ Text extraction
- ✓ Simple API
- ✓ Chainable
- ✓ No configuration
- ✓ Works with all models
- ✓ Automatic .content access
When NOT to Use
For structured output—use JsonOutputParser or .with_structured_output().
Notes
Streaming just works
StrOutputParser is stream-transparent. chain.stream({"question": q}) yields string chunks as the model produces them. Use "".join(chain.stream(input)) to reconstruct the full response or pass chunks to a websocket for real-time output.
No parsing, no validation
StrOutputParser does not validate or reformat the model's output. If the model returns malformed JSON when you expected structured data, it silently passes the broken string through. Use JsonOutputParser or model.with_structured_output() when structure matters.
Works with completion and chat models
StrOutputParser handles both BaseMessage (chat model output) and str (completion model output). It checks the type at runtime and returns .content for messages, or the string as-is. No configuration change is needed when switching model types.
Async streaming with astream
For async FastAPI endpoints, use async for chunk in chain.astream(input): yield chunk. StrOutputParser's async path is fully implemented and does not block the event loop during the streaming response.
Import
from langchain_core.output_parsers import StrOutputParser
Usage Examples
Use Parser
from langchain_core.output_parsers import StrOutputParser
chain = prompt | model | StrOutputParser()
result = chain.invoke({'input': 'data'})
print(result) # Plain string, not AIMessage
Streaming Output to Terminal
chain = prompt | model | StrOutputParser()
# Stream tokens as they arrive
for chunk in chain.stream({'question': 'What is RAG?'}):
print(chunk, end="", flush=True)
Async Streaming in FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
app = FastAPI()
@app.get('/stream')
async def stream_response(question: str):
async def gen():
async for chunk in chain.astream({'question': question}):
yield chunk
return StreamingResponse(gen(), media_type="text/plain")
Common Pitfalls
❌ Use StrOutputParser without a model that produces messages
✅ StrOutputParser works after any model.invoke()
Alternatives
| Class | When to Use |
|---|---|
| JsonOutputParser | For structured JSON output |
Related LangChain References
Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with StrOutputParser and the wider framework.
StrOutputParser FAQ
What is StrOutputParser in LangChain?
Extract text from model output. StrOutputParser is the simplest LangChain output parser. It takes an AIMessage (the output of a chat model) or a string (from a completion model) and returns the text content as a plain Python string. It is most commonly used as the final step in an LCEL chain: chain = prompt | model | StrOutputParser(). Without StrOutputParser, invoking a chat model returns an AIMessage object—you would need to access .content manually. StrOutputParser is stateless and has no configuration …
Which package provides StrOutputParser?
DevShelfHub documents StrOutputParser from the langchain-core package. Pin your installed LangChain version and match imports to the snippet on this page.
When should I use StrOutputParser?
You just need the text response. Use StrOutputParser at the end of chains for simple text output.
When should I avoid using StrOutputParser?
For structured output—use JsonOutputParser or .with_structured_output().
How do I import StrOutputParser in Python?
from langchain_core.output_parsers import StrOutputParser
Where can I explore more LangChain API reference pages?
Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.