alibaba/spring-ai-alibaba · warning
会话不存在
Error message
会话不存在: {} What it means
ChatSessionServiceImpl.getSession looks up a session in the in-memory sessionStore by id. If the id is missing/blank it returns null quietly; if no session exists for the id it logs '会话不存在' (session not found) and returns null. Callers must handle a null return.
Solutions
- Verify the sessionId passed by the client is the one returned from session creation
- Persist sessions in a shared store (Redis/DB) or enable sticky sessions for multi-instance deployments
- Treat null return as 'session not found' and create a new session instead of proceeding
- Check session expiry settings (isSessionExpired) — the session may have been evicted
Example fix
// before
ChatSession s = service.getSession(sessionId);
s.getMessages(); // NPE if not found
// after
ChatSession s = service.getSession(sessionId);
if (s == null) { s = service.createSession(); } Defensive patterns
Strategy: type-guard
Validate before calling
if (sessionId == null || sessionId.isBlank()) throw new IllegalArgumentException("sessionId is required"); Type guard
ChatSession s = service.getSession(sessionId);
if (s == null) { /* treat as not-found: create new or return 404 */ } Try / catch
ChatSession s = service.getSession(sessionId);
if (s == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("session not found: " + sessionId);
} Prevention
- Reuse the exact sessionId returned at creation time
- Use sticky sessions or a shared session store behind multiple instances
- Refresh session ids after server restarts; don't hardcode ids in tests
- Check expiry windows if sessions disappear under load
When it happens
Trigger: session -> getSession with a sessionId that was never created, was already deleted, or belonged to a different server instance (in-memory store, not shared across replicas or lost on restart).
Common situations: Client retrying with an old sessionId after server restart; load balancer routing to a replica that doesn't hold the session; expired sessions evicted; hardcoded/stale session ids in tests.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/dc85e9f26da3fc1a.
Report an issue: GitHub.
Appendix: source
Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/ChatSessionServiceImpl.java:77
public ChatSession createEvaluatorSession(String prompt, String variables, String modelConfig) {
String sessionId = UUID.randomUUID().toString();
ModelConfigInfo modelConfigInfo = modelConfigParser.checkAndGetModelConfigInfo(modelConfig);
ChatSession session = ChatSession.builder().template(prompt).variables(variables).modelConfig(modelConfigInfo)
.sessionId(sessionId).createTime(System.currentTimeMillis()).lastUpdateTime(System.currentTimeMillis())
.build();
sessionStore.put(sessionId, session);
return session;
}
@Override
public ChatSession getSession(String sessionId) {
if (sessionId == null || sessionId.trim().isEmpty()) {
return null;
}
ChatSession session = sessionStore.get(sessionId);
if (session == null) {
log.warn("会话不存在: {}", sessionId);
return null;
}
// 检查会话是否过期
if (isSessionExpired(session)) {
log.info("会话已过期,删除: {}", sessionId);
sessionStore.remove(sessionId);
return null;
}
return session;
}
@Override
public void updateSession(ChatSession session) {
if (session != null && session.getSessionId() != null) {
session.setLastUpdateTime(System.currentTimeMillis());
sessionStore.put(session.getSessionId(), session);View on GitHub (pinned to f82da0b50f)