🚀 Add MinifiedPydanticOutputParser to Optimize Token Usage in LLM Outputs
Here's a new class: MinifiedPydanticOutputParser, a drop-in replacement for PydanticOutputParser that automatically reduces token usage by replacing field names with short identifiers (e.g., a, b, c, …), while preserving full descriptions and reversibility.
✨ What It Does
- Transforms a given Pydantic schema by replacing verbose field names with shorter aliases.
- Retains all Field(..., description=...) information — essential for prompt construction and LLM understanding.
- Accepts minified JSON outputs from the LLM and reconstructs the original schema transparently.
- Supports nested models and list fields recursively.
- Compatible with strict=True mode used with with_structured_output.
✅ Benefits
- Reduces prompt and completion token count, leading to faster LLM response times and lower inference costs.
- Maintains clarity in the LLM's understanding of the field semantics thanks to preserved descriptions.
- No code change required downstream — consumers receive the original schema post-parsing.
📉 Performance Impact
In personal benchmarks, this optimization led to a ~30% reduction in LLM response time, due to:
- Fewer tokens needing generation
- Reduced I/O and parsing overhead
💸 This also translates to lower API costs, especially in high-throughput or large-output scenarios.
Examples
🧪 What it does
Given:
class User(BaseModel):
first_name: str = Field(..., description="The user's first name")
last_name: str = Field(..., description="The user's last name")
The model is transformed into:
class MinifiedUser(BaseModel):
a: str = Field(..., description="The user's first name")
b: str = Field(..., description="The user's last name")
Then seamlessly restored to the original User class after parsing the LLM output.
Metadata
Release files for langchain-pydantic-minifier 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_pydantic_minifier-1.0.2.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_pydantic_minifier-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / langchain_pydantic_minifier-1.0.2.tar.gz
| Download URL | langchain_pydantic_minifier-1.0.2.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a86572c36827eab67c681afd095be42fa18f288f13936bc14878b89917c2438f
|
|
BLAKE2b-256 checksum How to use checksums |
2f399fb71d9b1aba5fc75b3616d2a313d15db5686261187f1ea7f4e60bc01013
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1015-azure
|
Release files / langchain_pydantic_minifier-1.0.2-py3-none-any.whl
| Download URL | langchain_pydantic_minifier-1.0.2-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fcba3ff73390fe6f33a4f2fb6e3c062b9e3680505dc01eb61568175984d8fbe
|
|
BLAKE2b-256 checksum How to use checksums |
08eee9467beab070fbecddbfe2de620d3c84d3b7488b79de018726c75dcb3e77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1015-azure
|