{"record":{"id":"5a57435af9f53dd4","repo":"spring-projects/spring-ai","slug":"context-does-not-have-an-entry-for-key-s","errorCode":null,"errorMessage":"Context does not have an entry for key [%s]","messagePattern":"Context does not have an entry for key \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-model/src/main/java/org/springframework/ai/model/AbstractResponseMetadata.java","lineNumber":64,"sourceCode":"\t * @param key key\n\t * @param <T> value type\n\t * @return entry or {@code null} if not present\n\t */\n\tpublic <T> @Nullable T get(String key) {\n\t\treturn (T) this.map.get(key);\n\t}\n\n\t/**\n\t * Gets an entry from the context. Throws exception when entry is not present.\n\t * @param key key\n\t * @param <T> value type\n\t * @return entry\n\t * @throws IllegalArgumentException if not present\n\t */\n\tpublic <T> T getRequired(Object key) {\n\t\tT object = (T) this.map.get(key);\n\t\tif (object == null) {\n\t\t\tthrow new IllegalArgumentException(\"Context does not have an entry for key [\" + key + \"]\");\n\t\t}\n\t\treturn object;\n\t}\n\n\t/**\n\t * Checks if context contains a key.\n\t * @param key key\n\t * @return {@code true} when the context contains the entry with the given key\n\t */\n\tpublic boolean containsKey(Object key) {\n\t\treturn this.map.containsKey(key);\n\t}\n\n\t/**\n\t * Returns an element or default if not present.\n\t * @param key key\n\t * @param defaultObject default object to return\n\t * @param <T> value type","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-model/src/main/java/org/springframework/ai/model/AbstractResponseMetadata.java#L46-L82","documentation":"AbstractResponseMetadata.getRequired(key) retrieves a metadata entry and throws IllegalArgumentException when the key is absent (or mapped to null). It is a contract that the caller requires the value; use get(key) or containsKey(key) when absence is acceptable.","triggerScenarios":"Calling getRequired(\"some-key\") on response metadata (usage, model info, etc.) where the provider response did not include that key — e.g. requesting a field a particular model/provider never populates.","commonSituations":"Reading provider-specific metadata keys that not all models return; expecting a key under one name while the library stores it under another; older provider versions omitting fields newer code requires.","solutions":["Check containsKey(key) before calling getRequired, or use the nullable get(key) instead","Use the exact key constant defined by the library (e.g. AbstractUsage.REQUEST_LIMIT_REMAINING) rather than a hand-typed string","Verify the model/provider actually returns this metadata field; switch or upgrade provider/library if the field is genuinely missing"],"exampleFix":"// before\nInteger tokens = metadata.getRequired(\"total_tokens\");\n// after\nInteger tokens = metadata.containsKey(\"total_tokens\") ? metadata.getRequired(\"total_tokens\") : null;","handlingStrategy":"try-catch","validationCode":"if (!responseMetadata.containsKey(key)) {\n    throw new IllegalStateException(\"Metadata missing required key: \" + key);\n}","typeGuard":null,"tryCatchPattern":"try {\n    T value = metadata.getRequired(key);\n} catch (IllegalArgumentException e) {\n    // fall back to default or log missing metadata key\n    T value = defaultValue;\n}","preventionTips":["Prefer get(key) when the value is optional","Use the library's key constants instead of literal strings","Check containsKey before requiring provider-specific metadata fields"],"tags":["metadata","missing-key","illegal-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}