liveBook · Manning

Hallucination

As explained in Starting Data Analytics with Generative AI and Python

A generative-AI behavior in which a model produces plausible but unsupported, invented, or inaccurate information.

Can you trust a plausible AI answer when it informs a business decision? A hallucination is output that sounds coherent but is not grounded in the prompt or the model’s training information. A model may fill gaps with invented facts, numbers, references, or details about your data instead of acknowledging uncertainty. section 8.3 section 1.1

Hallucination describes the model’s behavior; AI delusion describes a user’s unquestioning acceptance of its output. An answer can also be wrong without being a hallucination: if incorrect training data says the sky is green, repeating that claim is a bad-data problem. Neither distinction makes the answer safe to use without checking it. section 8.3

An invented DataFrame in an otherwise useful analysis

Suppose you ask an AI assistant for Python code to compare average product price, freight value, and payment value by category and region. It returns a pandas groupby workflow using merged_data. But your actual DataFrame is df_merged, and the two structures differ significantly. Merely swapping the names would still produce an error. That visible failure is easier to catch than code that runs smoothly against the wrong data and returns misleading results. section 3.3.2

import pandas as pd
# Assuming you have already merged the DataFrames and named it 
'merged_data'

# Group the data by product category and region (state)
grouped_data = merged_data.groupby(
['product_category_name_english', 'customer_state'])

# Calculate the average product price, freight value, 
and payment value for each group
average_values = grouped_data[
['price', 'freight_value', 'payment_value']].mean()

# Reset the index to have a flat DataFrame
average_values = average_values.reset_index()

# Rename the columns for clarity
average_values.columns = ['product_category', 'state', 'avg_product_price', 
'avg_freight_value', 'avg_payment_value']

# You can now explore the 'average_values' DataFrame 
to analyze the differences across product categories and regions

Inspect the generated grouping code: its first assumption is that a DataFrame named merged_data already exists. Check that assumption and the available columns before running or adapting it. section 3.3.2

Treat the response as a proposal to verify, not a finished finding:

  • Compare generated names and structures with the DataFrames you actually have; run and test generated code before relying on its results. section 3.3.2 section 3.5
  • If a long conversation has lost track of your data, provide a detailed reminder or start a new conversation. section 3.3.2
  • Use specialist knowledge to refine answers about niche domains, and avoid basing critical decisions on an unsupported recommendation. section 1.2.3
  • Use higher API temperature cautiously in production: greater response randomness brings a higher hallucination risk. section 1.3.3

The cost of skipping verification can extend beyond analytics. Fictitious cases and rulings appeared in AI-generated court submissions that were not cross-checked. Present AI-assisted results in context so unsupported claims do not travel further up a decision chain as established facts. section 8.4.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines the behavior, distinguishes it from AI delusion and bad training data, and gives verification practices rather than model internals.
Examples
Uses an invented pandas DataFrame, a green-sky training-data scenario, and unverified fictitious court cases.
What it assumes
Defines hallucination and AI delusion from zero; assumes familiarity with DataFrames for the Python analytics example.
What you can do after
Diagnose/debug with it by checking generated data assumptions, testing code, and cross-verifying claims before relying on results.
How it connects
Connects hallucination to AI delusion, training data quality, domain knowledge limits, context loss, and code testing.

Also explained in

Compare how 29 books explain hallucination →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.