{"record":{"id":"97ec38ae38cfd0d6","repo":"alibaba/spring-ai-alibaba","slug":"error-97ec38","errorCode":null,"errorMessage":"会话 {} 不存在，创建新会话","messagePattern":"会话 (.+?) 不存在，创建新会话","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/PromptRunServiceImpl.java","lineNumber":133,"sourceCode":"    /**\n     * 获取或创建会话\n     */\n    private ChatSession getOrCreateSession(PromptRunRequest request) {\n        // 如果强制创建新会话或没有提供sessionId，创建新会话\n        if (Boolean.TRUE.equals(request.getNewSession()) || request.getSessionId() == null || request.getSessionId()\n                .trim().isEmpty()) {\n            return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),\n                    request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());\n        }\n        \n        // 尝试获取现有会话\n        ChatSession existingSession = chatSessionService.getSession(request.getSessionId());\n        if (existingSession != null) {\n            return existingSession;\n        }\n        \n        // 会话不存在，创建新会话\n        log.warn(\"会话 {} 不存在，创建新会话\", request.getSessionId());\n        return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),\n                request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());\n    }\n    \n    /**\n     * 生成真实的AI流式响应\n     *\n     * @param session 会话对象\n     * @return 流式响应\n     */\n    private Flux<PromptRunResponse> generateRealAIResponse(ChatSession session, PromptRunRequest request) {\n        // 用于收集完整响应的容器\n        AtomicReference<StringBuilder> completeResponse = new AtomicReference<>(new StringBuilder());\n        AtomicReference<ChatMessageMetrics> metrics = new AtomicReference<>(ChatMessageMetrics.builder().build());\n        \n        String fullPrompt = modelConfigParser.replaceVariables(session.getTemplate(), session.getVariables());\n        Map<String, String> observationMetadata = new HashMap<>(4);\n        if (StringUtils.hasText(request.getPromptKey()) && !\"playground\".equals(request.getPromptKey())) {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/PromptRunServiceImpl.java#L115-L151","documentation":"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.","triggerScenarios":"Calling the prompt run/stream API with a sessionId that was never created or has expired/been deleted.","commonSituations":"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.","solutions":["Create the session first via the session API and use the returned id in subsequent prompt-run calls","Persist and reuse the session id returned by createSession on the client side","If silent recreation is undesired, check session existence before running and surface an error instead","Verify the session store backend still holds the session (restarts may clear in-memory sessions)"],"exampleFix":"// before\nrequest.setSessionId(\"stale-id\"); // may silently create a new session\nrunPrompt(request);\n// after\nChatSession s = chatSessionService.getSession(sessionId);\nif (s == null) { s = chatSessionService.createSession(...); sessionId = s.getId(); }\nrequest.setSessionId(sessionId);","handlingStrategy":"validation","validationCode":"ChatSession s = chatSessionService.getSession(request.getSessionId()); if (s == null) { s = createSession(); request.setSessionId(s.getId()); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create a session first and reuse the returned id","Persist session ids client-side across requests","Remember server restarts/TTL can invalidate sessions"],"tags":["session","fallback","state-management"],"backgroundTag":"record-not-found","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}