alibaba/spring-ai-alibaba · info

会话 {} 不存在,创建新会话

Error message

会话 {} 不存在,创建新会话

What it means

In PromptRunServiceImpl.getOrCreateSession, when chatSessionService.getSession(sessionId) returns null, this warning is logged and a brand-new session is created via createSessionWithMockTools instead of failing. The client-supplied session id does not reference an existing session, so a fresh one is silently 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/PromptRunServiceImpl.java:133

    /**
     * 获取或创建会话
     */
    private ChatSession getOrCreateSession(PromptRunRequest request) {
        // 如果强制创建新会话或没有提供sessionId,创建新会话
        if (Boolean.TRUE.equals(request.getNewSession()) || request.getSessionId() == null || request.getSessionId()
                .trim().isEmpty()) {
            return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),
                    request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());
        }
        
        // 尝试获取现有会话
        ChatSession existingSession = chatSessionService.getSession(request.getSessionId());
        if (existingSession != null) {
            return existingSession;
        }
        
        // 会话不存在,创建新会话
        log.warn("会话 {} 不存在,创建新会话", request.getSessionId());
        return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),
                request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());
    }
    
    /**
     * 生成真实的AI流式响应
     *
     * @param session 会话对象
     * @return 流式响应
     */
    private Flux<PromptRunResponse> generateRealAIResponse(ChatSession session, PromptRunRequest request) {
        // 用于收集完整响应的容器
        AtomicReference<StringBuilder> completeResponse = new AtomicReference<>(new StringBuilder());
        AtomicReference<ChatMessageMetrics> metrics = new AtomicReference<>(ChatMessageMetrics.builder().build());
        
        String fullPrompt = modelConfigParser.replaceVariables(session.getTemplate(), session.getVariables());
        Map<String, String> observationMetadata = new HashMap<>(4);
        if (StringUtils.hasText(request.getPromptKey()) && !"playground".equals(request.getPromptKey())) {

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Create the session first via the session API and use the returned id in subsequent prompt-run calls
  2. Persist and reuse the session id returned by createSession on the client side
  3. If silent recreation is undesired, check session existence before running and surface an error instead
  4. Verify the session store backend still holds the session (restarts may clear in-memory sessions)

Example fix

// before
request.setSessionId("stale-id"); // may silently create a new session
runPrompt(request);
// after
ChatSession s = chatSessionService.getSession(sessionId);
if (s == null) { s = chatSessionService.createSession(...); sessionId = s.getId(); }
request.setSessionId(sessionId);
Defensive patterns

Strategy: validation

Validate before calling

ChatSession s = chatSessionService.getSession(request.getSessionId()); if (s == null) { s = createSession(); request.setSessionId(s.getId()); }

Prevention

When it happens

Trigger: Calling the prompt run/stream API with a sessionId that was never created or has expired/been deleted.

Common situations: Client caching a stale session id after server restart or session TTL expiry; typos in the session id; tests/integration runs reusing ids from a cleaned database.

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/97ec38ae38cfd0d6. Report an issue: GitHub.