Repository: langchain4j/langchain4j (2026-04-02T10:19:48Z)

Issue:
[BUG] RunningOllamaModel misses field for contextLength
<!-- Please provide as many details as possible, this will help us to deliver a fix as soon as possible. Thank you! -->

Ollama provides the `context_length` as a response when listing running models:
https://docs.ollama.com/api/ps#response-models-items-context-length

The class dev.langchain4j.model.ollama.RunningOllamaModel does not have the corresponding `contextLength` field.

**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

The information is silently dropped and thus missing in the RunningOllamaModel object.

**Log and Stack trace**
<!-- Please provide a log and a stack trace (with exception), if applicable. -->

**To Reproduce**
<!-- Please provide a relevant code snippets to reproduce this bug. -->

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->

The information should be stored in the RunningOllamaModel object.

**Please complete the following information:**
- LangChain4j version: 1.12.2
- LLM(s) used: 
- Java version: 
- Spring Boot version (if applicable): 

**Additional context**
<!-- Add any other context about the problem here. -->



Discussion:
@ap-actoron thank you for reporting, feel free to open a PR
Hi @dliubarskyi and @ap-actoron, if @ap-actoron is not planning to work on this or is currently busy, I'd be happy to submit a quick PR to add the missing contextLength field!



Patch:
diff --git a/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/RunningOllamaModel.java b/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/RunningOllamaModel.java
index 59b4c6044c8..e03b3aa6462 100644
--- a/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/RunningOllamaModel.java
+++ b/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/RunningOllamaModel.java
@@ -1,18 +1,17 @@
 package dev.langchain4j.model.ollama;
 
+import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
+
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.databind.PropertyNamingStrategies;
+import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.annotation.JsonNaming;
-
 import java.time.OffsetDateTime;
 
-import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
-
 @JsonIgnoreProperties(ignoreUnknown = true)
 @JsonInclude(NON_NULL)
-@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
+@JsonNaming(SnakeCaseStrategy.class)
 public class RunningOllamaModel {
 
     private String name;
@@ -20,14 +19,24 @@ public class RunningOllamaModel {
     private Long size;
     private String digest;
     private OllamaModelDetails details;
+
     @JsonDeserialize(using = OllamaDateDeserializer.class)
     private OffsetDateTime expiresAt;
-    private Long sizeVram;
-
-    RunningOllamaModel() {
-    }
 
-    RunningOllamaModel(String name, String model, Long size, OllamaModelDetails details, String digest, OffsetDateTime expiresAt, Long sizeVram) {
+    private Long sizeVram;
+    private Integer contextLength;
+
+    RunningOllamaModel() {}
+
+    RunningOllamaModel(
+            String name,
+            String model,
+            Long size,
+            OllamaModelDetails details,
+            String digest,
+            OffsetDateTime expiresAt,
+            Long sizeVram,
+            Integer contextLength) {
         this.name = name;
         this.model = model;
         this.size = size;
@@ -35,6 +44,7 @@ public class RunningOllamaModel {
         this.digest = digest;
         this.expiresAt = expiresAt;
         this.sizeVram = sizeVram;
+        this.contextLength = contextLength;
     }
 
     public String getName() {
@@ -93,6 +103,14 @@ public void setSizeVram(Long sizeVram) {
         this.sizeVram = sizeVram;
     }
 
+    public Integer getContextLength() {
+        return contextLength;
+    }
+
+    public void setContextLength(Integer contextLength) {
+        this.contextLength = contextLength;
+    }
+
     static Builder builder() {
         return new Builder();
     }
@@ -106,6 +124,7 @@ static class Builder {
         private OllamaModelDetails details;
         private OffsetDateTime expiresAt;
         private Long sizeVram;
+        private Integer contextLength;
 
         Builder name(String name) {
             this.name = name;
@@ -142,8 +161,13 @@ Builder sizeVram(Long sizeVram) {
             return this;
         }
 
+        Builder contextLength(Integer contextLength) {
+            this.contextLength = contextLength;
+            return this;
+        }
+
         RunningOllamaModel build() {
-            return new RunningOllamaModel(name, model, size, details, digest, expiresAt, sizeVram);
+            return new RunningOllamaModel(name, model, size, details, digest, expiresAt, sizeVram, contextLength);
         }
     }
 }


Test patch:
diff --git a/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaModelsIT.java b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaModelsIT.java
index f704c73dc92..92e9bd74327 100644
--- a/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaModelsIT.java
+++ b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaModelsIT.java
@@ -92,6 +92,7 @@ void should_return_list_of_running_models() {
             assertThat(runningOllamaModel.getName()).contains(TINY_DOLPHIN_MODEL);
             assertThat(runningOllamaModel.getDigest()).isNotBlank();
             assertThat(runningOllamaModel.getExpiresAt()).isNotNull();
+            assertThat(runningOllamaModel.getContextLength()).isPositive();
         });
     }
 }
diff --git a/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/RunningOllamaModelTest.java b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/RunningOllamaModelTest.java
new file mode 100644
index 00000000000..e0d22bcf0e5
--- /dev/null
+++ b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/RunningOllamaModelTest.java
@@ -0,0 +1,44 @@
+package dev.langchain4j.model.ollama;
+
+import static dev.langchain4j.model.ollama.OllamaJsonUtils.fromJson;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+class RunningOllamaModelTest {
+
+    @Test
+    void should_deserialize_context_length_from_running_models_response() {
+        String json = """
+                {
+                  "models": [
+                    {
+                      "name": "gemma3",
+                      "model": "gemma3",
+                      "size": 6591830464,
+                      "digest": "a2af6cc3eb7fa8be8504abaf9b04e88f17a119ec3f04a3addf55f92841195f5a",
+                      "details": {
+                        "format": "gguf",
+                        "family": "gemma3",
+                        "families": [
+                          "gemma3"
+                        ],
+                        "parameter_size": "4.3B",
+                        "quantization_level": "Q4_K_M"
+                      },
+                      "expires_at": "2025-10-17T16:47:07.93355-07:00",
+                      "size_vram": 5333539264,
+                      "context_length": 4096
+                    }
+                  ]
+                }
+                """;
+
+        RunningModelsListResponse response = fromJson(json, RunningModelsListResponse.class);
+
+        assertThat(response.getModels()).singleElement().satisfies(model -> {
+            assertThat(model.getName()).isEqualTo("gemma3");
+            assertThat(model.getContextLength()).isEqualTo(4096);
+        });
+    }
+}