Walkthrough: Vision receipts & media studio
Two practical samples illustrate opposite ends of multimodal agents in PiSharp:
16-ReceiptToExpenseaccepts a receipt image (--image receipt.png), enforces strict integer-cent arithmetic in C#, and rejects unbalanced extractions before expense classification.27-ProductContentStudiodrafts constrained product copy offline and adds an approval-gated image generation path powered byPiSharp.AI.ModelOperations.GenerateImagesAsync.
Part 1: Multimodal input & integer cents (16-ReceiptToExpense)
Section titled “Part 1: Multimodal input & integer cents (16-ReceiptToExpense)”Run it
Section titled “Run it”# Offline OCR-text fixture demo (yields balanced $17.82 Cedar Cafe Meals expense)dotnet run --project samples/agents/16-ReceiptToExpense -- --workspace artifacts/receipt-demo
# Deterministic self-tests (checks fractional cent rejection and unbalanced sum rejection)dotnet run --project samples/agents/16-ReceiptToExpense -- --self-test
# Live vision extraction on a real PNG, JPEG or WebP receipt photodotnet run --project samples/agents/16-ReceiptToExpense -- --live --api openai-responses --model gpt-6.2-sol --image path/to/receipt.png --workspace artifacts/receipt-liveWhy the schema swaps tools in image mode
Section titled “Why the schema swaps tools in image mode”When --image is omitted, Workflow.Create registers parse_receipt_text against the fixture’s OCR text. When --image is supplied (which requires --live), SampleHost attaches the validated image bytes (up to 20 MiB) to the native multimodal request and swaps in capture_receipt:
new("capture_receipt", "Submit merchant, date, totalCents, and line charges with integer amountCents read from the attached receipt image. " + "Use cents (17.82 becomes 1782), not fractional dollar numbers.", SampleJson.Schema(""" { "merchant": {"type":"string"}, "date": {"type":"string"}, "totalCents": {"type":"integer","minimum":1,"maximum":9000000000000}, "charges": { "type":"array", "items": { "type":"object", "properties": {"label":{"type":"string"},"amountCents":{"type":"integer","minimum":0,"maximum":9000000000000}}, "required":["label","amountCents"], "additionalProperties":false } } } """, "merchant", "date", "totalCents", "charges"), (a, _) => Task.FromResult<JsonNode?>(Capture(c, a)))Two deliberate engineering choices keep vision extraction reliable:
- Integer cents instead of floats:
type: "integer"prevents IEEE-754 rounding drift and letsSchemaValidationreject fractional cents (1782.5) before the handler runs. - Sum verification before classification:
Capturesums everyamountCentsentry and rejects the receipt ("Charges do not sum to total") if it differs fromtotalCentsby more than 1 cent, prompting the vision model to re-check tax or line items beforeclassify_expensecan run.
Part 2: Opt-in image generation (27-ProductContentStudio)
Section titled “Part 2: Opt-in image generation (27-ProductContentStudio)”Run it
Section titled “Run it”# Offline fixture demo (writes product-copy.json and illustration-specification visual-asset.json)dotnet run --project samples/agents/27-ProductContentStudio -- --workspace artifacts/studio-demo
# Offline self-test (exercises ModelOperationApis.GenerateImagesAsync against a fake HttpMessageHandler)dotnet run --project samples/agents/27-ProductContentStudio -- --self-test
# Opt-in live image generation via OpenRouter (requires OPENROUTER_API_KEY + --generate-image + --approve)dotnet run --project samples/agents/27-ProductContentStudio -- --generate-image --provider openrouter --model IMAGE_MODEL --key-env OPENROUTER_API_KEY --approve --workspace artifacts/studio-liveCalling ModelOperationApis.GenerateImagesAsync safely
Section titled “Calling ModelOperationApis.GenerateImagesAsync safely”Inside Workflow.cs, prepare_visual_asset refuses to invoke paid image generation unless both --generate-image and --approve are set, verifies the returned MIME type (image/png, image/jpeg, image/webp) and byte ceiling (10 MiB), and redacts any error message via SecretRedactor.Redact:
var model = new ImageModel(c.Model, c.Model, OpenRouterImages.ApiId, "openrouter", string.IsNullOrWhiteSpace(c.BaseUrl) ? "https://openrouter.ai/api/v1" : c.BaseUrl, ["text"], new ModelCost(0, 0, 0, 0), ["image"]);
var output = await ModelOperationApis.GenerateImagesAsync(model, new ImagesContext([new ImagesTextBlock(brief)]), new ImagesOptions { ApiKey = apiKey, Http = http, MaxRetries = 0 }, ct);And notice how --self-test verifies the entire request/response/base64-decode pipeline offline by injecting a fake HttpMessageHandler into ImagesOptions.Http — zero live API calls required in CI.
Try breaking it
Section titled “Try breaking it”- Try
--imagewithout--liveon sample 16: verify it fails fast (Image extraction requires --live and a vision-capable model.) rather than silently pretending OCR ran. - Try
--generate-imagewithout--approveon sample 27: confirmprepare_visual_assetblocks the paid image API call. - Explore
PiSharp.AI.Mediaand 1.6.0FitImages: for video, TTS speech, STT transcription, embeddings, and reranking, see Media library; rundotnet run --project samples/Durable.Examples -- 47-fit-imagesto see automatic image fitting for durable tool results.