Python / Python Modern Generative AI and Agents Interview Questions
How do you reliably get structured JSON output from LLMs, and what tools does LangChain provide?
Getting LLMs to reliably return structured data (not just text) is essential for applications that need to parse and act on model outputs. Three complementary approaches exist: prompt-level instructions, API-level enforcement (JSON mode / structured outputs), and library-level output parsers with validation and retry.
# ── Approach 1: OpenAI structured outputs (most reliable)
from pydantic import BaseModel, Field
from openai import OpenAI
client = OpenAI()
class JobPosting(BaseModel):
company: str = Field(description='Company name')
role: str = Field(description='Job title')
years_exp: int = Field(description='Years of experience required')
skills: list[str] = Field(description='Required technical skills')
text = 'Acme Corp is hiring a senior ML engineer with 5+ years, Python, PyTorch.'
completion = client.beta.chat.completions.parse(
model='gpt-4o',
messages=[{'role': 'user', 'content': f'Extract info from: {text}'}],
response_format=JobPosting,
)
job = completion.choices[0].message.parsed
print(type(job)) # <class '__main__.JobPosting'> — a real Pydantic model
print(job.company) # Acme Corp
print(job.skills) # ['Python', 'PyTorch']
# ── Approach 2: LangChain with_structured_output
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(model='gpt-4o')
structured_llm = llm.with_structured_output(JobPosting) # wraps with schema
prompt = ChatPromptTemplate.from_template('Extract info from: {text}')
chain = prompt | structured_llm
result = chain.invoke({'text': text})
print(result.company, result.years_exp) # Acme Corp 5
# ── Approach 3: PydanticOutputParser with retry
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate
parser = PydanticOutputParser(pydantic_object=JobPosting)
prompt_with_format = ChatPromptTemplate.from_template(
'Extract info from: {text}\n\n{format_instructions}'
).partial(format_instructions=parser.get_format_instructions())
chain2 = prompt_with_format | ChatOpenAI(model='gpt-4o-mini') | parser
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
