Codex agent in Jupyter AI, explanations in nbinlineai
This lesson places Jupyter AI’s Codex chat beside nbinlineai’s inline AI questions. Follow Jupyter AI’s setup guide to install the official @agentclientprotocol/codex-acp adapter and Codex CLI, then confirm codex login status before starting JupyterLab. The Codex persona uses that separate chat and CLI login.
Codex agent in Jupyter AI, explanations in nbinlineai
This lesson places Jupyter AI’s Codex chat beside nbinlineai’s inline AI questions. Follow Jupyter AI’s setup guide to install the official @agentclientprotocol/codex-acp adapter and Codex CLI, then confirm codex login status before starting JupyterLab. The Codex persona uses that separate chat and CLI login.
For the two inline Markdown questions, connect ChatGPT or an API provider through nbinlineai’s Configure AI panel. Choose the inline connection in this notebook’s AI defaults row. One connection does not configure the other. This notebook stores no credentials or Jupyter AI chat transcript. It does include two completed nbinlineai answers beside the deliberately unfixed code.
Run the three Python cells in order. The draft deliberately ignores zero values and groups containing only missing values. Its diagnostic reports the mismatches without stopping the notebook. Use the Jupyter AI chat prompts to ask Codex to repair that one function and rerun the diagnostic. Keep this as a step-by-step exercise.
Jupyter AI’s agent single-cell command executes Python cells. Run inline Markdown questions with nbinlineai’s Run button or JupyterLab’s native Shift+Enter and Run All.
# Synthetic daily revenue in dollars: 0.0 is a known zero; None is missing.
sales = [
{'region': 'North', 'revenue': 10.0},
{'region': 'North', 'revenue': 0.0},
{'region': 'North', 'revenue': None},
{'region': 'North', 'revenue': 20.0},
{'region': 'South', 'revenue': 5.0},
{'region': 'South', 'revenue': None},
{'region': 'South', 'revenue': 15.0},
{'region': 'South', 'revenue': 0.0},
{'region': 'East', 'revenue': None},
]
print(f'{len(sales)} synthetic records in three regions')
9 synthetic records in three regions
defsummarize_revenue(rows):
"""Draft summary: count, total, and mean of known revenue by region."""
grouped = {}
for row in rows:
amount = row['revenue']
if amount: # BUG: a known zero is false, but should count.
region = row['region']
bucket = grouped.setdefault(region, {'count': 0, 'total': 0.0})
bucket['count'] +=1
bucket['total'] += amount
for bucket in grouped.values():
bucket['mean'] = bucket['total'] / bucket['count']
return grouped
print(summarize_revenue(sales))
from math import isclose
expected = {
'North': {'count': 3, 'total': 30.0, 'mean': 10.0},
'South': {'count': 3, 'total': 20.0, 'mean': 20.0/3.0},
'East': {'count': 0, 'total': 0.0, 'mean': None},
}
defcheck_summary():
"""Report every spec mismatch; return True once the draft is fixed."""
original = [row.copy() for row in sales]
actual = summarize_revenue(sales)
problems = []
if sales != original:
problems.append('input records changed')
ifset(actual) !=set(expected):
problems.append(f'regions: expected {sorted(expected)}, got {sorted(actual)}')
for region, want in expected.items():
got = actual.get(region)
if got isNone:
continueif got.get('count') != want['count']:
problems.append(f'{region} count: expected {want["count"]}, got {got.get("count")}')
ifnot isclose(got.get('total', float('nan')), want['total']):
problems.append(f'{region} total: expected {want["total"]}, got {got.get("total")}')
mean = got.get('mean')
if (mean isNone) != (want['mean'] isNone) or (mean isnotNoneandnot isclose(mean, want['mean'])):
problems.append(f'{region} mean: expected {want["mean"]}, got {mean}')
if problems:
print('Needs work:')
for problem in problems:
print(' -', problem)
returnFalseprint('All checks pass: zero values count, missing values do not, empty groups remain, and input is unchanged.')
returnTrue
checks_pass = check_summary()
Needs work:
- regions: expected ['East', 'North', 'South'], got ['North', 'South']
- North count: expected 3, got 2
- North mean: expected 10.0, got 15.0
- South count: expected 3, got 2
- South mean: expected 6.666666666666667, got 10.0
Ask the Codex agent in Jupyter AI chat
Open Jupyter Chat and select the Codex persona. If it asks you to log in, complete codex login in a terminal for the same environment and restart JupyterLab. Agent tool permissions belong to Jupyter AI. Copy each prompt into chat, not into an nbinlineai AI Prompt cell. The agent should touch only the draft function cell and run only the three named Python cells in order.
1. Diagnose before editing
In the open notebook, inspect the Python cells with IDs revenue-data, revenue-summary-draft, and revenue-checks. Explain why the current summary miscounts known zero revenue and omits East. Do not edit any cell or run any notebook command yet.
2. Fix and verify
In this notebook, edit only the Python cell with ID revenue-summary-draft. Make summarize_revenue count 0.0 as known revenue, ignore None, retain every region including East, and use mean=None when a region has no known values. Preserve the count/total/mean output shape and do not change the input rows. Then run only the Python code cells revenue-data, revenue-summary-draft, and revenue-checks in that order. Report the check cell's actual output. Do not use Run All; do not run, edit, or delete either nbinlineai AI Prompt Markdown cell.
If the agent proposes a change without applying it, make the edit yourself and run the three Python cells. The diagnostic should say All checks pass. If it does not, inspect the function and retry the checks. Save the notebook after reviewing the change.
Ask nbinlineai to explain the result
The next two cells are nbinlineai AI Prompt Markdown cells. Their own Run buttons use the selected nbinlineai connection; Codex ACP login alone cannot answer them. Both questions already have saved inline answers. The first diagnoses the unfixed draft, and the second asks a tutoring question.
These answers do not record a Codex repair; the optional Jupyter AI agent exercise above is separate. To request a fresh answer after editing the draft, turn off Keep answer for that question and run it with your configured connection. The second prompt uses the Learning style. The current live code and earlier notebook text can be used as context according to your Context controls; check those controls before sending if the agent made edits.
Explain in plain language how summarize_revenue now treats 0.0 and None differently. Use the North and East records as examples, and explain what the check cell verifies. If the draft has not been fixed yet, say that clearly rather than describing an imagined fix.
The supplied draft has not been fixed: if amount skips both 0.0 and None. It therefore counts only North’s 10.0 and 20.0, and omits East entirely because its only value is None.
The check cell verifies that known zeros count, missing values do not, East remains with count 0 and mean None, the totals and means are correct, and the input records are unchanged.
Continue as a learner
Read the saved first inline answer and the saved Learning answer below. Try answering the Learning question yourself before changing the function. To get a fresh tutoring turn, turn off Keep answer for the Learning prompt and run it with your configured inline connection. You can edit this Markdown prompt and its paired answer as ordinary notebook notes. Earlier completed inline question/answer pairs can enter later context; Jupyter AI chat history remains in its separate chat file.
Ask me one question that would help me reason about adding a region with two missing values and one known zero. Wait for my attempt before giving a full answer. Which assertions would best show that zero and missing values still have different meanings?
Suppose a new region has revenues [None, None, 0.0]. What would you assert about its presence in the summary and its count, total, and mean? Which of those assertions would fail if the zero were treated as missing? Try writing them before we check together.