Skip to main content

automatically reduces token usage by replacing field names with short identifiers (e.g., a, b, c, …), while preserving full descriptions and reversibility.

Project description

🚀 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.

MinifiedPydanticOutputParser

✨ 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_pydantic_minifier-0.2.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_pydantic_minifier-0.2.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file langchain_pydantic_minifier-0.2.2.tar.gz.

File metadata

  • Download URL: langchain_pydantic_minifier-0.2.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1015-azure

File hashes

Hashes for langchain_pydantic_minifier-0.2.2.tar.gz
Algorithm Hash digest
SHA256 59c6f31877367ebfc57c8a01c2a8e5c1a2b610369e39ad2a6a607d8b02d107b4
MD5 f63ba489e45445f97c85e8c70e9c9a3e
BLAKE2b-256 b6afd3d4badcec56eae1aefa4786deea9a792020d750990416c1cfae042b8be1

See more details on using hashes here.

File details

Details for the file langchain_pydantic_minifier-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_pydantic_minifier-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 837e7593a7c9c8bba8dfb0d76b036b9363fb2fb2a394e0f0ada26aa0797c5efd
MD5 ab512b61205532a82f5b0aaa05fe25c9
BLAKE2b-256 cc9def9802b9e8991410e21de084b4fec7a51ba25607059a5aa6606b14ce736c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page