Foundry Local SDK
Foundry Local SDK for Python provides a control-plane interface to Foundry Local, a unified on-device AI runtime that enables local generative AI inference. It allows developers to interact with locally run models, perform chat completions, and manage local AI resources without network latency or per-token costs. The current version is 1.0.0, and it has moved to General Availability, suggesting a stable, but potentially evolving, release cadence.
Common errors
-
ConnectionRefusedError: [Errno 111] Connection refused
cause The Foundry Local runtime service is not running or is not accessible at its default port (usually 8008 or similar). The Python SDK cannot connect to the local AI engine.fixStart the Foundry Local runtime application/service. Verify its status using the `foundry status` CLI command. Ensure no firewall is blocking access to its local port. -
foundry.exceptions.ModelNotFoundError: Model 'my-non-existent-model' not found
cause You are trying to use a model name that has not been downloaded to your local Foundry Local instance, or the model name is misspelled.fixDownload the required model using the Foundry Local CLI (`foundry download <model_name>`) or `client.download_model(<model_name>)` via the SDK. Verify the exact model name from `foundry list`. -
ImportError: cannot import name 'FoundryLocal' from 'foundry_local_sdk'
cause Incorrect import statement. The top-level package for `foundry-local-sdk` exposes the main class via `foundry` not `foundry_local_sdk`.fixChange your import statement to `from foundry import FoundryLocal`. -
ValueError: max_tokens must be positive
cause The `max_tokens` parameter for `chat.completions.create` was provided with a non-positive value (e.g., 0 or a negative number).fixEnsure `max_tokens` is set to a positive integer value greater than 0, corresponding to the maximum number of tokens you want the model to generate.
Warnings
- gotcha The `foundry-local-sdk` Python library is a client to the Foundry Local runtime. It requires the Foundry Local application/service to be installed and running on your system for any API calls to function.
- gotcha Models used with the SDK must be downloaded to your local Foundry Local instance. Attempting to use a model that hasn't been downloaded will result in a `ModelNotFoundError`.
- gotcha Early versions (pre-1.0.0, e.g., v0.8.113) had a limitation of supporting only one tool call per request. While this might be improved in newer versions, be aware of potential limitations with complex tool-calling scenarios.
- gotcha Some users experienced issues with tool calling on NVIDIA GPUs with older Foundry Local runtime versions (e.g., v0.8.117). This indicates potential hardware-specific runtime bugs.
Install
-
pip install foundry-local-sdk
Imports
- FoundryLocal
from foundry_local_sdk import FoundryLocal
from foundry import FoundryLocal
Quickstart
from foundry import FoundryLocal
import os
# Ensure the Foundry Local runtime is installed and running.
# For example, download the 'phi3-mini-4k-instruct' model via the Foundry Local CLI:
# foundry download phi3-mini-4k-instruct
try:
client = FoundryLocal()
# You might need to explicitly download a model if not already present.
# client.download_model("phi3-mini-4k-instruct") # Uncomment and run once if needed
print("FoundryLocal client initialized. Attempting chat completion...")
response = client.chat.completions.create(
model="phi3-mini-4k-instruct", # Replace with a model you have downloaded
messages=[
{"role": "user", "content": "What is the capital of France?"}
],
max_tokens=50
)
print("Response:", response.choices[0].message.content)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure the Foundry Local runtime is installed, running, and the specified model is downloaded.")