preparePromptForStructuredOutput static method

String preparePromptForStructuredOutput({
  1. required String prompt,
  2. required String jsonSchema,
})

Two-step prompt preparation: ask commons to format prompt with the supplied jsonSchema BEFORE invoking the LLM. Returns the schema- augmented system prompt. Mirrors Swift's RunAnywhere+StructuredOutput.swift preparePrompt(prompt:options:) helper used inside generateWithStructuredOutput.

Falls back to prompt verbatim when commons returns an empty system prompt or the ABI is unavailable. Throws SDKException.notInitialized if SDK is not initialized; throws SDKException.processingFailed on non-zero commons error.

Implementation

static String preparePromptForStructuredOutput({
  required String prompt,
  required String jsonSchema,
}) {
  if (!DartBridge.isInitialized) throw SDKException.notInitialized();

  final options = StructuredOutputOptions(
    schema: JSONSchema(rawJson: jsonSchema),
    jsonSchema: jsonSchema,
    includeSchemaInPrompt: true,
  );
  final result = DartBridgeStructuredOutput.shared.preparePrompt(
    prompt: prompt,
    options: options,
  );
  if (result.errorCode != 0) {
    // Mirrors Swift `.processingFailed` on prep failure.
    throw SDKException.processingFailed(
      'preparePromptForStructuredOutput failed (rc=${result.errorCode})',
    );
  }
  final systemPrompt = result.systemPrompt;
  return systemPrompt.isNotEmpty ? systemPrompt : prompt;
}