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

This PR introduces 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.

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.1.5.tar.gz (4.8 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.1.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_pydantic_minifier-0.1.5.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.9.21 Darwin/24.5.0

File hashes

Hashes for langchain_pydantic_minifier-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9d547825fcbbb32142305efabc2ccbc49988f5a88a82a6e8c322b9d282d1db55
MD5 720e9b1f200bfc090f2a4221cad74170
BLAKE2b-256 88f2c008bb37984fce88cf8c76b951ca8d81049b0277ed7a1ce2a2be3698c49f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langchain_pydantic_minifier-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 93087a9262655072a1de5d20740a7b4c51d53393fbf4f3d27699d02d178ffe06
MD5 ee6524e4300faa5077075cebd8d00dbc
BLAKE2b-256 4e0cc72c255867ba855a5dc8fabbe8f9846a074a6a35975bea0c08c233fb8d11

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