Overview

4 Using a Coding Companion with Python

In this chapter, the focus shifts from using AI to help with SQL toward using a coding companion for Python-based data engineering work. The main idea is that Python opens up many more kinds of tasks than querying alone, including API calls, JSON parsing, and regex-based text processing. Because these workflows are often repetitive, messy, and prone to errors, they are especially well suited to AI assistance when the prompts are clear and the results are tested carefully.

The chapter first shows how AI can help with API interactions using simple public services like NumbersAPI. A coding companion can generate boilerplate code to fetch data, parse JSON, handle failures, add logging, and store results in a pandas DataFrame. It also demonstrates how iterative prompting improves the output, such as adding retry logic or using uploaded API documentation to guide the model toward more accurate request URLs and parameters. At the same time, the chapter warns that AI may overengineer solutions, misunderstand prompt context, or misread documentation, so developers must verify outputs and shape prompts to match their actual environment.

Next, the chapter explains how AI helps flatten nested JSON into analysis-ready tabular data and how to use regex for extracting and normalizing phone numbers. With JSONPlaceholder, the examples show how to safely pull shallow and deeply nested fields, combine values into cleaner columns, and handle missing data with chained lookups. For regex, the chapter walks through extracting U.S. phone numbers, converting them to a standard format, splitting them into structured fields, and even exporting them as JSON for document-oriented workflows. The chapter ends with a lab that applies these ideas to the Open Brewery DB API, reinforcing that AI is most useful as a drafting and refinement partner, while human review remains essential for correctness and robustness.

Interacting with an API: This diagram shows how a client constructs a full request URL by combining a base endpoint with query parameters that define the data request (e.g., filters, limits, and format). The server receives the request, handles tasks like authentication and data lookup, and returns a structured JSON response. This client-server pattern is central to data engineering workflows, where APIs often serve as the primary source of external or cloud-based data. Knowing how to shape requests and interpret responses is essential for working with modern data pipelines.
Result of the zero-shot prompt response against the NumbersAPI
Result of the one-shot prompt response against the NumbersAPI
Result of the API documentation enhanced response against the NumbersAPI
Result of the simple JSON unpacking response against the JSONPlaceholder API.
Result of the complex JSON unpacking response against the JSONPlaceholder API.
Structured breakdown of phone numbers using regex and pandas
Results for Lab Answers 1
Results for Lab Answers 2
Results for Lab Answers 3

FAQ

What is the main purpose of using a coding companion with Python in this chapter?It helps you write, refine, and iterate on Python code faster for common data engineering tasks such as API calls, JSON parsing, and regex-based text processing.
Why is Python especially well-suited for AI-assisted data engineering work?Python is flexible and works well for tedious, repetitive, and error-prone tasks like handling APIs, nested JSON, and complex regular expressions, making it a strong fit for iterative AI prompting.
How can an AI coding companion help when interacting with APIs?It can generate boilerplate request code, help parse JSON responses, handle errors, summarize documentation, and speed up building scripts that fetch and store API data.
What is NumbersAPI and why is it used in this chapter?NumbersAPI is a free, simple API that returns trivia, math, date, or year facts about numbers. It is used because it is public, easy to query, and ideal for learning API interactions and JSON parsing.
Why did the first AI-generated API script use logging instead of print statements, and why might that be a problem?The AI overengineered the solution by choosing file-based logging, which is useful in production but less convenient for notebooks where immediate, in-line feedback from print statements is often preferred.
What does few-shot prompting mean in the context of refining Python code?It means continuing the conversation with the AI and improving the code step by step—for example, adding retry logic, better error handling, or more visible progress messages after an initial working script.
How does uploading API documentation improve the quality of AI-generated code?It gives the model direct access to the API’s actual parameters, endpoints, and response format, which can reduce hallucinations and improve accuracy when generating code for more complex or private APIs.
What is a common risk when using AI with API documentation?The AI may misread the docs, ignore required parameters, invent unsupported ones, or use incorrect endpoints, so the generated code still needs to be verified against the real documentation or tested with tools like curl or Postman.
How does the chapter use AI to help with nested JSON data?It shows how AI can generate Python code to safely access nested fields, flatten structures into pandas DataFrames, combine values like latitude and longitude, and handle missing data with chained .get() calls.
What are the main regex tasks covered in this chapter?The chapter shows how AI can help extract U.S. phone numbers from text, normalize them into a consistent format like +1-XXX-XXX-XXXX, split them into components, and output the results as a DataFrame or JSON.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free