alibaba/spring-ai-alibaba · error · RuntimeException

会话不存在:

Error message

会话不存在: 

What it means

ChatSessionServiceImpl.getOrCreateSessionChatClient looks up the ChatSession for the given sessionId inside computeIfAbsent and throws this RuntimeException when getSession returns null. Without a session there is no bound model config, so no ChatClient can be created.

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:140

        });
        
        if (cleanedCount[0] > 0) {
            log.info("清理了 {} 个过期会话", cleanedCount[0]);
        }
    }
    
    @Override
    public ChatClient getSessionChatClient(String sessionId) {
        return sessionClients.get(sessionId);
    }
    
    
    @Override
    public ChatClient getOrCreateSessionChatClient(String sessionId, Map<String, String> observationMetadata) {
        return sessionClients.computeIfAbsent(sessionId, key -> {
            ChatSession session = getSession(sessionId);
            if (session == null) {
                throw new RuntimeException("会话不存在: " + sessionId);
            }
            return chatClientFactoryDelegate.createChatClient(session.getModelConfig().getModelId(),
                    session.getModelConfig().getParameters(), observationMetadata);
        });
    }
    
    /**
     * 检查会话是否过期
     */
    private boolean isSessionExpired(ChatSession session) {
        long currentTime = System.currentTimeMillis();
        return (currentTime - session.getLastUpdateTime()) > SESSION_EXPIRE_TIME;
    }
    
    /**
     * 获取当前会话总数(用于监控)
     */
    public int getSessionCount() {

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Verify the sessionId was created first (call the create-session endpoint) and that the exact value is passed
  2. Recreate the session if the server restarted and the in-memory sessionClients/session store was cleared
  3. Check the session persistence store (repository/DB) to confirm the session record still exists
  4. In client code, handle this error by creating a new session and retrying the request

Example fix

// before
ChatClient c = service.getOrCreateSessionChatClient(oldSessionId, meta); // session lost after restart
// after
ChatSession s = service.getSession(oldSessionId);
if (s == null) {
    String newId = service.createSession(request); // recreate, then get client
}
ChatClient c = service.getOrCreateSessionChatClient(newId, meta);
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: check session existence before requesting a client
ChatSession s = chatSessionService.getSession(sessionId);
if (s == null) {
    sessionId = chatSessionService.createSession(newSessionRequest); // recreate first
}

Type guard

boolean sessionExists(ChatSession s) { return s != null && s.getModelConfig() != null && s.getModelConfig().getModelId() != null; }

Try / catch

try {
    client = service.getOrCreateSessionChatClient(sessionId, metadata);
} catch (RuntimeException e) {
    if (e.getMessage().startsWith("会话不存在")) {
        sessionId = createNewSession(); // recreate and retry once
    }
}

Prevention

When it happens

Trigger: Calling getOrCreateSessionChatClient(sessionId, ...) with a sessionId that was never created, was deleted, or whose storage lookup fails — including inside a computeIfAbsent where the throw propagates as a CompletionException-like wrap to the caller.

Common situations: Client sends a stale sessionId after server restart (in-memory session store lost); sessions expired/cleaned up while the front end still holds the id; typo or truncation in sessionId passed from the request.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/06687256ddb3df63. Report an issue: GitHub.