Monday, 1 June 2026

llm se structure output nikalwane ke liye

 Haan, PydanticOutputParser to ek standard tarika hai. Lekin jo technique tum shayad yaad kar rahe ho wo OutputFixingParser ya RetryOutputParser ho sakti hai.

Jab model structured output support nahi karta (ya galat JSON de deta hai), tab ye parsers kaafi useful hote hain.

1. OutputFixingParser

Agar model ne almost sahi output diya hai but formatting thodi kharab hai, to ye automatically LLM se fix karwa deta hai.

from langchain.output_parsers import OutputFixingParser
from langchain_core.output_parsers import PydanticOutputParser

parser = PydanticOutputParser(pydantic_object=Person)

fixing_parser = OutputFixingParser.from_llm(
    parser=parser,
    llm=model
)

chain = template | model | fixing_parser

result = chain.invoke({"place": "indian"})
print(result)

Example

Model output:

Name: Raj
Age: 25
City: Delhi

Parser ko JSON chahiye tha.

OutputFixingParser internally LLM ko bolta hai:

"Is output ko required JSON format me convert karo"

Aur phir return karta hai:

Person(
    name="Raj",
    age=25,
    city="Delhi"
)

2. RetryOutputParser

Ye aur powerful hai.

Agar parsing fail ho jaye to original prompt + model output dono lekar model ko dubara bhejta hai.

from langchain.output_parsers import RetryOutputParser

parser = PydanticOutputParser(pydantic_object=Person)

retry_parser = RetryOutputParser.from_llm(
    parser=parser,
    llm=model
)

prompt_value = template.invoke({"place": "indian"})

response = model.invoke(prompt_value)

result = retry_parser.parse_with_prompt(
    response.content,
    prompt_value
)

print(result)

Internal Flow

Prompt
   ↓
LLM
   ↓
Bad Output
   ↓
Parse Failed
   ↓
RetryOutputParser
   ↓
LLM ko original prompt + error bhejta hai
   ↓
Correct JSON
   ↓
Pydantic Object

3. Sabse modern tarika (Recommended)

Aajkal LangChain me generally:

structured_llm = model.with_structured_output(Person)

result = structured_llm.invoke(
    "Generate a fictional Indian person"
)

print(result)

Lekin ye tabhi kaam karega jab underlying model/tool calling ya structured output support karta ho.


Agar model structured output support nahi karta

Meri preference:

parser = PydanticOutputParser(pydantic_object=Person)

fixing_parser = OutputFixingParser.from_llm(
    parser=parser,
    llm=model
)

chain = prompt | model | fixing_parser

Kyuki:

  • Simple hai

  • Retry logic mil jata hai

  • JSON formatting issues handle karta hai

  • Open-source models (Llama, Mistral, Gemma etc.) ke saath bhi kaafi achha chalta hai

Aur agar tum LangChain v0.3+/LCEL use kar rahe ho to ek aur advanced pattern hai:

parser.get_format_instructions()
RetryOutputParser

Ye production me kaafi use hota hai jab local/open-source models se reliable structured JSON nikalwana ho.

No comments:

Post a Comment

Hello

llm se structure output nikalwane ke liye

 Haan, PydanticOutputParser to ek standard tarika hai. Lekin jo technique tum shayad yaad kar rahe ho wo OutputFixingParser ya RetryOutput...