SShortSingh.
Back to feed

Using Pydantic to Get Reliable JSON Output from an LLM in Python

0
·2 views

A small shop wants customer emails turned into structured order data for its spreadsheet, but language model outputs can break in several ways. Common failures include stray text around the JSON, wrong types such as writing a quantity as a word, and invented details like order numbers absent from the email. The guide separates format errors, which parsers and schemas catch, from factual errors, which require checking the output against the source text. It recommends a layered approach: clear prompts, schema validation with Pydantic, retries that feed errors back to the model, source checks, and escalation to a human after repeated failures. It also notes that fixed-template inputs may be better served by forms or regular expressions than by a language model.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

AdversaryGate patches six releases in four days after fixing fail-open merge-gate bugs

The author of AdversaryGate, a merge gate for coding-agent patches, shipped versions 3.0.0 through 3.0.6 after repeatedly finding ways the gate could wrongly approve changes. The bugs shared a pattern: an input the gate did not measure was silently skipped, letting the rest of the patch decide the verdict. Fixes included detecting tests that read code instead of running it, catching comment-only and no-op edits, refusing binary-marked diffs, and mutating every changed line and branch. Some changes, like handling the newline-at-EOF marker and raising timeouts and memory limits, addressed honest patches wrongly marked INCONCLUSIVE instead. Two releases, 3.0.2 and 3.0.3, were published with an API token, so versions before 3.0.4 were removed and releases now require signed tags and PyPI attestations.

0
ProgrammingDEV Community ·

Developer Builds Automated Real Estate Data Pipeline Across 14 Houston Counties

A technical write-up describes how a developer integrated CrawlOS with Supabase to automate property data collection across 14 Houston-area counties. The system uses pg_cron for nightly job scheduling and scrapes Harris County Appraisal District records to produce roughly 125 qualified leads per run. The article covers the database schema design, crawler deployment patterns, and lead scoring logic. It also discusses challenges encountered when moving the prototype to production on self-hosted infrastructure.

0
ProgrammingDEV Community ·

Bienestar digital con IA: predecir el exceso de uso antes de que ocurra

The article argues that common screen-time apps like Screen Time, Digital Wellbeing, Freedom and Opal rely on static limits, reactive alerts and easily disabled blocks. It proposes a system built on an XGBoost forecaster that predicts next-day app usage and estimates when a user will hit a limit, along with a binge-risk score explained via SHAP. A transparent controller would gradually reduce usage toward a goal without cutting more than 10% per week, and an LLM coach would intervene at critical moments. The author reports a usage forecast R² of about 0.77 and a binge-risk AUC of about 0.79, which he deliberately left below higher benchmarks rather than manipulating the data.

0
ProgrammingDEV Community ·

Competitor prices vary by surface and fees, so one price per product misleads

A product's price is not a single figure, since the same item can cost different amounts on a retailer site, a marketplace, a grocery delivery service, and a delivery app. Each surface is set by different parties, including the seller's own margins, marketplace seller rules, and platform markups or fees. Storing one price field per product either overwrites useful data or averages it into a figure no customer actually paid. The author recommends recording each observation with its surface, location, fee components, and shopper context. Fee stacks can shrink or enlarge the gap between items: in one example, a 21.7% item-price gap narrowed to 9.1% once delivery and service fees were added.