Errors#
This page documents the exception classes for error handling.
Note
Swift Equivalent: GenerationError cooresponds to the the LanguageModelSession.GenerationError
in the Swift Foundation Models Framework. ToolCallError corresponds to the
LanguageModelSession.ToolCallError in
the Swift Foundation Models Framework. InvalidGenerationSchemaError is unique to the Python SDK
and does not have a direct Swift equivalent since it means a schema failed to compile in the underlying Swift.
Base Exceptions#
FoundationModelsError#
Generation Errors#
GenerationError#
- class apple_fm_sdk.GenerationError[source]#
Bases:
FoundationModelsErrorBase class for generation-specific errors.
Base class for errors that occur during text generation.
ExceededContextWindowSizeError#
- class apple_fm_sdk.ExceededContextWindowSizeError[source]#
Bases:
GenerationErrorError thrown when the context window size is exceeded.
Raised when the input exceeds the model’s context window size.
GuardrailViolationError#
- class apple_fm_sdk.GuardrailViolationError[source]#
Bases:
GenerationErrorError thrown when a guardrail violation occurs.
Raised when content violates safety guardrails. For more on the guardrails, see the Swift reference article on
SystemLanguageModelsafety meansures: Improving the safety of generative model output.
UnsupportedGuideError#
- class apple_fm_sdk.UnsupportedGuideError[source]#
Bases:
GenerationErrorError thrown when an unsupported guide is used.
Raised when an unsupported guide constraint is used.
UnsupportedLanguageOrLocaleError#
- class apple_fm_sdk.UnsupportedLanguageOrLocaleError[source]#
Bases:
GenerationErrorError thrown when an unsupported language or locale is used.
Raised when the requested language or locale is not supported.
DecodingFailureError#
- class apple_fm_sdk.DecodingFailureError[source]#
Bases:
GenerationErrorError thrown when decoding fails.
Raised when decoding the model’s output fails.
RateLimitedError#
- class apple_fm_sdk.RateLimitedError[source]#
Bases:
GenerationErrorError thrown when rate limited.
Raised when rate limits are exceeded. Rate limits do not apply to the on-device
SystemLanguageModelon macOS so you should not encounter this error.
ConcurrentRequestsError#
- class apple_fm_sdk.ConcurrentRequestsError[source]#
Bases:
GenerationErrorError thrown when there are too many concurrent requests.
Raised when too many concurrent requests are made. The python SDK does not enforce concurrency limits so you should not encounter this error.
RefusalError#
- class apple_fm_sdk.RefusalError[source]#
Bases:
GenerationErrorError thrown when the model refuses to generate content.
Raised when the model refuses to generate a response _specifically_ for safety reasons on a generable output. For more on refusals, see the Swift reference article on
SystemLanguageModelsafety meansures: Improving the safety of generative model output.Attributes:
reason(str): The reason for refusal
Schema Errors#
InvalidGenerationSchemaError#
- class apple_fm_sdk.InvalidGenerationSchemaError[source]#
Bases:
FoundationModelsErrorError thrown when a generation schema is invalid.
Raised when a generation schema is invalid. This means the schema failed to compile in the underlying Swift code and most commonly occurs if you give an invalid json schema to the
json_schemaparameter of therespond()method. This error may also occur if you use the@generabledecorator on a class with properties that cannot be serialized to a json schema.To validate if your json schema is valid, run the following code snippet in Swift:
import FoundationModels jsonSchemaString = "..." // Your JSON schema as a string let schema = try JSONDecoder().decode( GenerationSchema.self, from: Data(jsonSchemaString.utf8) )
The code above is the same validation step used in the Python SDK, so if it runs without throwing an error, your schema is valid.
For examples of what valid json schemas look like, see the tester schemas in the Github repo, such as
tests/tester_schemas/hedgehog.json.
Tool Errors#
ToolCallError#
- class apple_fm_sdk.ToolCallError[source]#
Bases:
FoundationModelsErrorError thrown when a tool call fails.
Raised when a
Toolcall fails.Attributes:
tool_name(str): Name of the tool that failedunderlying_error(Exception): The original exception