openChat method

  1. @nonVirtual
Future<InferenceChat> openChat({
  1. double? temperature,
  2. int? randomSeed,
  3. int? topK,
  4. double? topP,
  5. int tokenBuffer = 256,
  6. String? loraPath,
  7. bool? supportImage,
  8. bool? supportAudio,
  9. List<Tool> tools = const [],
  10. bool? supportsFunctionCalls,
  11. bool enableThinking = false,
  12. ModelType? modelType,
  13. ToolChoice toolChoice = ToolChoice.auto,
  14. int? maxFunctionBufferLength,
  15. String? systemInstruction,
  16. int? maxOutputTokens,
  17. ResponseFormat? responseFormat,
})

Opens a chat that owns an independent openSession and does not touch other open chats. Keep the returned chat and close it when done.

Each chat's own context-overflow rotation (_recreateSessionWithReducedChunks) creates a fresh sibling session via openSession, so peer chats are unaffected.

See openSession for the memory caveat. The returned chat is NOT stored in chat — that field tracks only the legacy createChat singleton. Hold the returned chat reference yourself and close it when done.

An unset value is filled from the model by resolveChatRequest, so a model whose type is not Gemma gets its own parsers here too.

responseFormat — what every reply must be, as on openSession: every session of the chat, its rebuilt ones too, is opened with it. A reply that fails the check is kept in the chat's history, then thrown (InferenceChat.generateChatResponse); a turn stopped before any text is taken back instead, its user message included. Checked before the engine is asked anything: ArgumentError together with tools or enableThinking: true (as the caller passed them), then ResponseFormatUnsupportedException when refusalFor refuses it; only then does the engine resolve the request.

Core implements this once for every engine (@nonVirtual); an engine supplies its defaults in resolveChatRequest and its per-turn behaviour in createChatTurnHooks.

Implementation

@nonVirtual
Future<InferenceChat> openChat({
  double? temperature,
  int? randomSeed,
  int? topK,
  double? topP,
  int tokenBuffer = 256,
  String? loraPath,
  bool? supportImage,
  bool? supportAudio,
  List<Tool> tools = const [],
  bool? supportsFunctionCalls,
  bool enableThinking = false,
  ModelType? modelType,
  ToolChoice toolChoice = ToolChoice.auto,
  int? maxFunctionBufferLength,
  String? systemInstruction,
  int? maxOutputTokens, // Cap GENERATED tokens (see createSession)
  ResponseFormat? responseFormat,
}) async {
  final format = _jsonSchemaFormat(responseFormat);
  if (format != null) {
    _checkFormatArguments(tools: tools, enableThinking: enableThinking);
    final refusal = refusalFor(format);
    if (refusal != null) throw refusal;
  }
  final resolved = resolveChatRequest(
    ChatRequest(
      temperature: temperature,
      randomSeed: randomSeed,
      topK: topK,
      topP: topP,
      tokenBuffer: tokenBuffer,
      loraPath: loraPath,
      supportImage: supportImage,
      supportAudio: supportAudio,
      tools: tools,
      supportsFunctionCalls: supportsFunctionCalls,
      enableThinking: enableThinking,
      modelType: modelType,
      toolChoice: toolChoice,
      maxFunctionBufferLength: maxFunctionBufferLength,
      systemInstruction: systemInstruction,
      maxOutputTokens: maxOutputTokens,
      responseFormat: format,
    ),
  );
  // Core owns the format: an engine that dropped or changed it would turn
  // the chat into one whose replies nobody checks.
  if (resolved.responseFormat != format) {
    throw StateError(
      '$runtimeType.resolveChatRequest changed the responseFormat '
      '(${resolved.responseFormat} instead of $format): it must carry '
      'ChatRequest.responseFormat over unchanged.',
    );
  }
  final independentChat = InferenceChat(
    sessionCreator: () => _openSession(
      SessionRequest(
        temperature: resolved.temperature,
        randomSeed: resolved.randomSeed,
        topK: resolved.topK,
        topP: resolved.topP,
        loraPath: resolved.loraPath,
        enableVisionModality: resolved.supportImage,
        enableAudioModality: resolved.supportAudio,
        systemInstruction: resolved.systemInstruction,
        enableThinking: resolved.enableThinking,
        tools: resolved.tools,
        maxOutputTokens: resolved.maxOutputTokens,
        modelType: resolved.modelType,
        toolChoice: resolved.toolChoice,
        responseFormat: format,
      ),
    ),
    maxTokens: maxTokens,
    tokenBuffer: resolved.tokenBuffer,
    supportImage: resolved.supportImage,
    supportAudio: resolved.supportAudio,
    supportsFunctionCalls: resolved.supportsFunctionCalls,
    maxFunctionBufferLength: resolved.maxFunctionBufferLength,
    tools: resolved.tools,
    enableThinking: resolved.enableThinking,
    modelType: resolved.modelType,
    fileType: fileType,
    toolChoice: resolved.toolChoice,
    // false means "not forced": the chat then derives it from the model's
    // function-call format, which is true for Gemma 4 and FunctionGemma.
    runtimeInjectsToolDeclarations: resolved.runtimeInjectsToolDeclarations
        ? true
        : null,
    systemInstruction: resolved.systemInstruction,
    turnHooks: createChatTurnHooks(resolved),
  );
  await independentChat.initSession();
  return independentChat;
}