New Tool Radar

AI Concepts

How to Extract Named Entities From Text Into Typed Structured Fields

Define an entity schema, send unstructured text, and check every extracted entity really appears in the source.

Schematic with three steps: Define the entity schema; Send the text with the schema; Verify each entity against the source.

Extracting information from unstructured text that conforms to a schema defined in code is one of the core uses of Structured Outputs: you define the fields once and the model fills them from the text. The published SDK example extracts entities into lists of attributes, colors and animals. The check that matters is verifying each extracted entity against the source text, because the model will try to fill the schema even when the input does not contain the entity.

What you need

  • Unstructured text to extract from
  • A schema listing the entity fields you need
  • The source text kept for the verification step

Step 1: Define the entity schema

Define the fields as typed lists, for example attributes, colors and animals as lists of strings. Only include fields the text can actually contain.

Step 2: Send the text with the schema

Send the unstructured text with the schema as the response format, and parse the typed result. Use streaming if you want to display fields as they arrive.

Step 3: Verify each entity against the source

Search the source text for every extracted entity. Any entity that does not appear in the source is a hallucination and must be dropped before the data is stored.

Worked example: synthetic entity extraction check

The table below is a synthetic example. It shows the expected result of verifying extracted entities; no API call was made.

Extracted entity In source text? Keep?
Attributes: red, round Yes Keep
Colors: red Yes Keep
Animals: cat Yes Keep
Animals: dog No – not in text Drop

Expected result (UNTESTED). 3 of 4 extracted entities appear in the source text and are kept; the dog does not appear and is dropped. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • The model will always try to adhere to the schema, which can result in hallucinations if the input is unrelated to the schema.
  • If the input cannot result in a valid response, the prompt should ask for empty parameters or a specific sentence instead.
  • Splitting a large text into simpler subtasks improves extraction accuracy.
  • Verify every extracted entity against the source before the data enters a database.

Sources

See all