Run this scenario cell by cell in a disposable JupyterLab project. The setup uses the owned runner’s NBINLINEAI_WORKED_ROOT when set, or asks for the absolute Jupyter server root and checks that it exists and contains this kernel’s working folder before making server-root-relative file references. Enter the folder JupyterLab serves, even when this notebook and its kernel are in a nested folder. Use a live Python kernel and selected ChatGPT connection.
From notebook output to an AI image question
Run this scenario cell by cell in a disposable JupyterLab project. The setup uses the owned runner’s NBINLINEAI_WORKED_ROOT when set, or asks for the absolute Jupyter server root and checks that it exists and contains this kernel’s working folder before making server-root-relative file references. Enter the folder JupyterLab serves, even when this notebook and its kernel are in a nested folder. Use a live Python kernel and selected ChatGPT connection.
A generated red output is exported, previewed, cropped, optionally saved, and attached to one question only after you click Attach image. The preflight question checks that no image was sent before that click. The saved worked run records a confirmed attachment and a final answer of red for the four-pixel image. After each browser operation finishes, inspect its receipt. Confirm your current image, then run the final question to repeat the example.
Make a disposable notebook output
from io import BytesIO
import hashlib
import os
from pathlib import Path, PurePosixPath
from PIL import Image as PILImage
from IPython.display import display, Image
from nbinlineai.tools import (list_outputs, export_output, open_media, close_media, crop_image, save_media, attach_media, release_media, operation_status)
root_text = os.environ.get('NBINLINEAI_WORKED_ROOT') orinput('Absolute Jupyter server root (the folder served by JupyterLab): ').strip()
root_choice = Path(root_text).expanduser()
ifnot root_choice.is_absolute():
raiseValueError('Enter an absolute Jupyter server-root folder; do not use the kernel working directory as a guess.')
server_root = root_choice.resolve(strict=True)
ifnot server_root.is_dir() ornot Path.cwd().resolve().is_relative_to(server_root):
raiseValueError('The server root must be an existing folder containing this kernel working directory.')
defserver_ref(path):
"""Make a server-root-relative path for an owned file or save destination."""
target = Path(path).resolve()
return target.relative_to(server_root).as_posix()
defserver_file(reference):
"""Map a server-returned relative path to the configured local root."""ifnotisinstance(reference, str) ornot reference or'\\'in reference:
raiseValueError('Expected a server-root-relative media path.')
relative = PurePosixPath(reference)
if relative.is_absolute() or relative.as_posix() != reference or reference =='.'or'..'in relative.parts:
raiseValueError('Media path must be a clean server-root-relative path.')
candidate = server_root / reference
ifnot candidate.resolve().is_relative_to(server_root):
raiseValueError('Media path escapes the configured server root.')
return candidate
source_image = PILImage.new('RGB', (8, 8), (212, 64, 47))
encoded = BytesIO(); source_image.save(encoded, format='PNG')
PNG_BYTES = encoded.getvalue()
print('Disposable 8x8 output image ready; no file saved.')
Disposable 8x8 output image ready; no file saved.
display(Image(data=PNG_BYTES))
Export the existing output
Run the list call, then its inspection cell after the browser answers. Export its exact output ID and revision into owned memory. Neither step reruns the image cell or creates a file.
Use &list_outputs for cell ID integration-output. Report only its immediate operation ID/state; do not invent an output ID or MIME type before the later status question.
Use &operation_status on the fresh operation ID in current live $current_list_operation_id from the direct list_outputs call above. The saved AI list_outputs answer belongs to an earlier run; do not query its historical ID. Report the actual output ID, revision, and MIME type of the generated red image only if this current operation completed. Do not rerun the output cell.
The current operation completed. The red image’s output ID is 3aad0bd3a931be2de51d15fef1f04aa3, revision 0, with MIME types image/png and text/plain.
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
The crop remains in owned memory and leaves the eight-pixel source unchanged. The later inspection checks actual decoded pixels and reports the exact encoded PNG hash.
This call writes a new server-root-relative PNG and keeps the in-memory derivative for the question. Its later cleanup removes only the generated saved path.
Run this question with the selected ChatGPT connection before the attachment step. The generated output and preview alone do not send a native image to the model.
Before I confirm any image attachment, do you receive a native image from this notebook? Answer briefly.
No. I don’t receive a native image until you explicitly attach it.
Confirm only the derivative for the next question
Run the call, click Attach image in the visible confirmation, then rerun the inspection cell until the receipt is complete. Confirmation changes only the identified question; it never runs it.
assert crop.status =='completed', 'Wait for the crop first.'
attached = attach_media(crop.media, 'integration-question', detail='auto')
attached
The saved answer below came from a confirmed attachment in the worked run. To repeat it, confirm your current four-pixel crop, check its indicator in context preview, then run this question with the selected ChatGPT connection. The model receives the crop as a native image item rather than base64 in tool text.
What is the visible color in the four-pixel-square image I explicitly attached?
Red.
Clean up owned memory and optional file
Run after the question. The first cell closes the preview and releases managed bytes. The final cell removes only this example’s generated saved path, if the optional save ran.
closed_preview = close_media(preview.result['preview_id']) if preview.status =='completed'elseNone
released_export = release_media(exported.media['media_id']) if exported.status =='completed'elseNone
released_crop = release_media(crop.media['media_id']) if crop.status =='completed'elseNoneprint('Cleanup requested; inspect the receipts later.')
Cleanup requested; inspect the receipts later.
print('Preview:', closed_preview.status if closed_preview else'none')
print('Export:', released_export.status if released_export else'none')
print('Crop:', released_crop.status if released_crop else'none')