diff --git a/LLama/Exceptions/RuntimeError.cs b/LLama/Exceptions/RuntimeError.cs
index 0feb53665..4db77911e 100644
--- a/LLama/Exceptions/RuntimeError.cs
+++ b/LLama/Exceptions/RuntimeError.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using LLama.Native;
namespace LLama.Exceptions;
@@ -56,4 +56,23 @@ public LLamaDecodeError(DecodeResult returnCode)
{
ReturnCode = returnCode;
}
+}
+
+///
+/// `llama_get_logits_ith` returned null, indicating that the index was invalid
+///
+public class GetLogitsInvalidIndexException
+ : RuntimeError
+{
+ ///
+ /// The incorrect index passed to the `llama_get_logits_ith` call
+ ///
+ public int Index { get; }
+
+ ///
+ public GetLogitsInvalidIndexException(int index)
+ : base($"llama_get_logits_ith({index}) returned null")
+ {
+ Index = index;
+ }
}
\ No newline at end of file
diff --git a/LLama/Native/SafeLLamaContextHandle.cs b/LLama/Native/SafeLLamaContextHandle.cs
index dee74f590..61c3c0bbc 100644
--- a/LLama/Native/SafeLLamaContextHandle.cs
+++ b/LLama/Native/SafeLLamaContextHandle.cs
@@ -472,6 +472,9 @@ public Span GetLogitsIth(int i)
unsafe
{
var logits = llama_get_logits_ith(this, i);
+ if (logits == null)
+ throw new GetLogitsInvalidIndexException(i);
+
return new Span(logits, model.VocabCount);
}
}