Tool catalog: attach an exact image

Run this notebook 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.

Tool catalog: attach an exact image

Run this notebook 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.

It creates a blue PNG, shows it, and binds its exact path and SHA-256 to one question only after you click Attach image. The AI tool question below also asks for an explicit confirmation; neither the call nor the click runs the target question. Inspect each receipt after the visible operation completes, then run the image question yourself.

Create a disposable still-image reference

from pathlib import Path, PurePosixPath
from uuid import uuid4
import os
import hashlib
from PIL import Image
from IPython.display import display
from nbinlineai.tools import attach_media, operation_status

root_text = os.environ.get('NBINLINEAI_WORKED_ROOT') or input('Absolute Jupyter server root (the folder served by JupyterLab): ').strip()
root_choice = Path(root_text).expanduser()
if not root_choice.is_absolute():
    raise ValueError('Enter an absolute Jupyter server-root folder; do not use the kernel working directory as a guess.')
server_root = root_choice.resolve(strict=True)
if not server_root.is_dir() or not Path.cwd().resolve().is_relative_to(server_root):
    raise ValueError('The server root must be an existing folder containing this kernel working directory.')

def server_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()

def server_file(reference):
    """Map a server-returned relative path to the configured local root."""
    if not isinstance(reference, str) or not reference or '\\' in reference:
        raise ValueError('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:
        raise ValueError('Media path must be a clean server-root-relative path.')
    candidate = server_root / reference
    if not candidate.resolve().is_relative_to(server_root):
        raise ValueError('Media path escapes the configured server root.')
    return candidate
source_path = Path.cwd() / f'browser-media-attachment-{uuid4().hex}.png'
Image.new('RGB', (2, 2), (35, 120, 210)).save(source_path, format='PNG')
source_ref = {'path': server_ref(source_path), 'sha256': hashlib.sha256(source_path.read_bytes()).hexdigest()}
print('Generated blue PNG:', source_path.name, source_ref['sha256'])
display(Image.open(source_path).resize((80, 80)))
Generated blue PNG: browser-media-attachment-e8782f267edf4c7691afc7ec4b0599ba.png b35ff24aed6413b07f72b30c35c05d73379d4491cbb533565ab78e0add7e3332

Use &attach_media with media=$source_ref, question_cell_id='attachment-question', and detail='auto'. Report its immediate operation ID and waiting/running state, then ask me to click Attach image. Do not claim confirmation or run the target question yet.

Operation 6g9-1PR2Hok1U-K8IddRrvOb165_0RhJ is waiting for your confirmation. Please click Attach image. I haven’t run the target question.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
attach_media receipt accepted {“media”:{“path”:“browser-media-attachment-db77ab223faa4541babe7fd719fcf7f1.png”,“sha256”:“b35ff24aed6413b07f72b30c35c05d73379d4491cbb533565ab78e0add7e3332”},“question_cell_id”:“attachment-question”,“detail”:“auto”} {“operation_id”:“6g9-1PR2Hok1U-K8IddRrvOb165_0RhJ”,“status”:“waiting_for_user”}

After I click Attach image, use &operation_status on the preceding AI attachment operation ID. Report the actual terminal state, question ID, and exact image SHA-256 only if confirmed; otherwise stop and retry later. Do not run the target question for me.

Confirmed: operation 6g9-1PR2Hok1U-K8IddRrvOb165_0RhJ completed. Question ID: attachment-question. Image SHA-256: b35ff24aed6413b07f72b30c35c05d73379d4491cbb533565ab78e0add7e3332. I did not run the target question.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
operation_status completed {“operation_id”:“6g9-1PR2Hok1U-K8IddRrvOb165_0RhJ”} {“operation_id”:“6g9-1PR2Hok1U-K8IddRrvOb165_0RhJ”,“status”:“completed”,“result”:{“confirmed”:true,“question_cell_id”:“attachment-question”,“image_sha256”:“b35ff24aed6413b07f72b30c35c05d73379d4491cbb533565ab78e0add7e3332”,“detail”:“auto”}}

attach_media — confirm the image for this question

Run the next code cell, then inspect its receipt in the following cell. Click Attach image in the visible confirmation. The receipt first reports waiting_for_user; after the click it reports completed with the exact SHA-256 and question ID. If you cancel, no attachment is recorded. Run the inspection cell again after the receipt changes.

from nbinlineai.tools import attach_media
attached = attach_media(source_ref, 'attachment-question', detail='auto')
attached
BrowserReceipt(operation_id=None, status='running')
attached.status, attached.operation_id, attached.result, attached.error
('completed',
 'ExvtG5dtMyREDoG5zqtVGEJCADk-1cYm',
 {'confirmed': True,
  'question_cell_id': 'attachment-question',
  'image_sha256': 'b35ff24aed6413b07f72b30c35c05d73379d4491cbb533565ab78e0add7e3332',
  'detail': 'auto'},
 None)

Run the confirmed image question

Open its context preview to check the image indicator. Preview does not run the model. Click Run on the question with the selected ChatGPT connection after the attachment receipt is complete; the model receives the image as a native image item. If the file or hash changes, confirm a new exact image instead of reusing an old attachment.

What color is the small square in my attached image? Please describe the visible color in one sentence.

The small square is blue.

Remove the disposable source after the question

Keep the PNG until you have finished previewing and running the question. The saved attachment points to this exact path and hash; removing it makes a later preview or rerun fail closed. Run this cleanup cell only after you are done.

assert source_path.exists()
source_path.unlink()
print('Disposable image removed:', not source_path.exists())
Disposable image removed: True

← Examples guide · Download this notebook