Skip to content

Fix samples to work with foundry-local-sdk 1.2.1#769

Open
samuel100 wants to merge 5 commits into
mainfrom
samuel100/samples-patches
Open

Fix samples to work with foundry-local-sdk 1.2.1#769
samuel100 wants to merge 5 commits into
mainfrom
samuel100/samples-patches

Conversation

@samuel100
Copy link
Copy Markdown
Contributor

Summary

Patches across Python / JS / C# / Rust samples so they run cleanly against the latest foundry-local-sdk 1.2.0 release. Verified by running every affected sample end-to-end on macOS (arm64).

Changes

Python — empty-choices guard on streaming chat (3 samples)

The final OpenAI SSE chunk now contains an empty choices array (just finish_reason and usage), which crashed three samples with IndexError on chunk.choices[0]. Added if not chunk.choices: continue guard.

  • samples/python/native-chat-completions/src/app.py
  • samples/python/tutorial-chat-assistant/src/app.py
  • samples/python/web-server/src/app.py

JS — API mismatches and readline EOF handling

  • samples/js/tutorial-tool-calling/app.jschatClient.completeChat in SDK 1.2.0 takes the tools array directly as its second argument, not wrapped in { tools }. Both call sites updated.
  • samples/js/tutorial-tool-calling/app.js and samples/js/tutorial-chat-assistant/app.jsaskQuestion now resolves to 'quit' when readline emits close (e.g. on stdin EOF) instead of throwing ERR_USE_AFTER_CLOSE.
  • samples/js/chat-and-audio-foundry-local/src/app.js — swap chat model from phi-3.5-mini to qwen2.5-0.5b so the sample runs against the smaller default catalog model.

C# — Betalgo package pin (4 tutorial samples fixed by one line)

samples/cs/Directory.Packages.props pinned Betalgo.Ranul.OpenAI = 9.2.0, but that version removed the ReasoningEfforts type and changed AudioCreateTranscriptionRequest.ResponseFormat. The 1.2.0 SDK reflects on these types in its JsonSerializationContext, throwing TypeLoadException/MissingMethodException. Pinned down to 9.1.0 (the version the SDK transitively requires), which fixes all 4 tutorial samples that explicitly depend on Betalgo.

Rust — share model cache across tutorials (4 samples)

Each Rust tutorial set its own app_name (tool-calling-app, note-taker, chat-assistant, doc-summarizer), which scopes the cache directory separately and forces re-download of already-cached models. Standardized on foundry_local_samples so the samples share the cache:

  • samples/rust/tutorial-tool-calling/src/main.rs
  • samples/rust/tutorial-voice-to-text/src/main.rs
  • samples/rust/tutorial-chat-assistant/src/main.rs
  • samples/rust/tutorial-document-summarizer/src/main.rs

Testing

Ran the full samples matrix on macOS arm64 against foundry-local-sdk 1.2.0:

Lang Passed Skipped Failed
Python 13 0 0
JS 11 4 0
C# 12 0 0
Rust 7 0 0 (3 unverified, source-only patches)

JS skips are for Windows-only WinML samples, an Electron GUI sample, and samples that pull phi-4-mini (not in the macOS test cache).

Note for macOS / Linux users: the JS samples should be installed with npm install --omit=optional to skip the foundry-local-sdk-winml postinstall, which fetches a Windows-only NuGet and leaves the SDK unable to locate Microsoft.AI.Foundry.Local.Core.dylib.

Patches across Python/JS/C#/Rust samples to bring them up to date with
the foundry-local-sdk 1.2.0 release.

Python — guard against empty `chunk.choices` on streaming chat completions.
The final OpenAI SSE chunk now contains an empty `choices` array (just
`finish_reason` and `usage`), which crashed three samples with
`IndexError` on `chunk.choices[0]`:
  * samples/python/native-chat-completions/src/app.py
  * samples/python/tutorial-chat-assistant/src/app.py
  * samples/python/web-server/src/app.py

JS — fix two API/UX bugs:
  * samples/js/tutorial-tool-calling/app.js — `chatClient.completeChat`
    in SDK 1.2.0 takes the tools array directly as its second argument,
    not wrapped in `{ tools }`. Both call sites updated.
  * samples/js/tutorial-tool-calling/app.js and
    samples/js/tutorial-chat-assistant/app.js — `askQuestion` now
    resolves to `'quit'` when readline emits `close` (e.g. on stdin
    EOF) instead of throwing `ERR_USE_AFTER_CLOSE`.
  * samples/js/chat-and-audio-foundry-local/src/app.js — swap chat
    model from `phi-3.5-mini` to `qwen2.5-0.5b` so the sample runs
    against the smaller default catalog model.

C# — pin Betalgo.Ranul.OpenAI to 9.1.0:
  * samples/cs/Directory.Packages.props — the 9.2.0 pin removed the
    `ReasoningEfforts` type and changed
    `AudioCreateTranscriptionRequest.ResponseFormat`, causing
    TypeLoadException/MissingMethodException in the 1.2.0 SDK's
    serialization context. Bumped down to 9.1.0 (matches the SDK's
    transitive requirement), fixing four tutorial samples.

Rust — share the model cache across tutorial samples:
  * samples/rust/tutorial-tool-calling/src/main.rs
  * samples/rust/tutorial-voice-to-text/src/main.rs
  * samples/rust/tutorial-chat-assistant/src/main.rs
  * samples/rust/tutorial-document-summarizer/src/main.rs

Each tutorial used its own `app_name` which scoped the cache directory
separately and forced re-download of already-cached models. Standardized
on `foundry_local_samples` so the samples reuse the shared cache.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 4, 2026 14:56
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
foundry-local Ready Ready Preview, Comment Jun 5, 2026 9:40am

Request Review

The sample reads `path.resolve("recording.mp3")` from the current
working directory. Ship the audio fixture (a copy of the audio file
already used by samples/python/audio-transcription/Recording.mp3) so
the sample is runnable out of the box.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates multi-language samples so they run cleanly against foundry-local-sdk 1.2.0 by addressing SDK surface changes and runtime behavior differences (streaming chunk shape, JS tools parameter shape, and sample dependency / cache configuration).

Changes:

  • Python: guard against streaming chunks that contain an empty choices array.
  • JS: align completeChat tool-calling signature and make readline input handling more resilient to stdin EOF/close.
  • C#/Rust: pin a compatible Betalgo package version for samples and standardize Rust app_name to share the model cache across tutorials.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
samples/rust/tutorial-voice-to-text/src/main.rs Standardizes Rust sample app_name to share the model cache.
samples/rust/tutorial-tool-calling/src/main.rs Standardizes Rust sample app_name to share the model cache.
samples/rust/tutorial-document-summarizer/src/main.rs Standardizes Rust sample app_name to share the model cache.
samples/rust/tutorial-chat-assistant/src/main.rs Standardizes Rust sample app_name to share the model cache.
samples/python/web-server/src/app.py Adds an empty-choices guard for streaming chat output.
samples/python/tutorial-chat-assistant/src/app.py Adds an empty-choices guard for streaming chat output.
samples/python/native-chat-completions/src/app.py Adds an empty-choices guard for streaming chat output.
samples/js/tutorial-tool-calling/app.js Updates tool-calling API usage and improves readline EOF handling (needs a small race-condition fix).
samples/js/tutorial-chat-assistant/app.js Improves readline EOF handling (needs a small race-condition fix).
samples/js/chat-and-audio-foundry-local/src/app.js Switches default chat model alias to qwen2.5-0.5b.
samples/cs/Directory.Packages.props Pins Betalgo dependency to a version compatible with the samples/SDK.

Comment thread samples/js/tutorial-tool-calling/app.js Outdated
Comment thread samples/js/tutorial-chat-assistant/app.js
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Mirror the autofix applied to tutorial-chat-assistant in 938f4d3:
the `rl.closed` check leaves a race window where rl can close before
`rl.question()` runs, and `rl.question()` throws synchronously on a
closed readline interface. Wrap the call in try/catch and resolve to
'quit' so stdin EOF is handled reliably.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@samuel100 samuel100 changed the title Fix samples to work with foundry-local-sdk 1.2.0 Fix samples to work with foundry-local-sdk 1.2.1 Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants