Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Add new SystemPrompt type support to OpenAI Model provider #90

Description

@Unshure

Overview

Add new SystemPrompt type support to OpenAI Model provider.

Background

The systemPrompt type was recently updated from a simple string to SystemPrompt = string | SystemContentBlock[] to support advanced features like prompt caching with cache points. The Bedrock model provider has been updated to support this new type, and now the OpenAI model provider needs the same update.

Implementation Requirements

Technical Context

  • Type Definition: SystemPrompt = string | SystemContentBlock[] (defined in src/types/messages.ts)
  • SystemContentBlock: TextBlock | CachePointBlock
  • Reference Implementation: Bedrock model provider (src/models/bedrock.ts, lines 352-372)
  • Target File: src/models/openai.ts (specifically the _formatRequest method, lines 400-407)

Required Changes

1. Update _formatRequest Method in src/models/openai.ts

The current implementation (lines 400-407) only handles string systemPrompt:

if (options?.systemPrompt && options.systemPrompt.trim().length > 0) {
  request.messages.push({
    role: 'system',
    content: options.systemPrompt,
  })
}

New Implementation Must:

  1. Handle String SystemPrompt (existing behavior):

    • Continue to work as-is for backward compatibility
    • Validate non-empty after trimming
  2. Handle Array SystemPrompt (new behavior):

    • Check if options.systemPrompt is an array
    • Extract text from all TextBlock elements
    • Concatenate text blocks to preserve structure
    • Add warning when CachePointBlock elements are present
    • Skip adding system message if array is empty or contains no text
  3. Cache Point Handling:

    • Emit console.warn when cache points are encountered
    • Warning message: "Cache points are not supported in OpenAI system prompts and will be ignored."
    • Rationale: OpenAI's Chat Completions API doesn't support prompt caching like Bedrock

2. Add Tests in src/models/__tests__/openai.test.ts

Follow the test patterns established in src/models/__tests__/bedrock.test.ts (lines 774-870):

Required Test Cases:

  1. Array with text blocks only:

    • Multiple text blocks should be concatenated
    • Verify system message content is correctly formatted
  2. Array with cache points:

    • Should warn about unsupported cache points
    • Should extract and use only text blocks
    • Verify warning message is logged
  3. Empty array:

    • Should not add system message to request
    • Verify no system message in formatted request
  4. Array with single text block:

    • Should work correctly as valid system prompt

Implementation Approach

Pattern to Follow:

// Pseudo-code based on Bedrock pattern
if (options?.systemPrompt !== undefined) {
  if (typeof options.systemPrompt === 'string') {
    // Existing string handling
    if (options.systemPrompt.trim().length > 0) {
      request.messages.push({
        role: 'system',
        content: options.systemPrompt,
      })
    }
  } else if (options.systemPrompt.length > 0) {
    // New array handling
    const textBlocks: string[] = []
    let hasCachePoints = false
    
    for (const block of options.systemPrompt) {
      if (block.type === 'textBlock') {
        textBlocks.push(block.text)
      } else if (block.type === 'cachePointBlock') {
        hasCachePoints = true
      }
    }
    
    if (hasCachePoints) {
      console.warn('Cache points are not supported by OpenAI and will be ignored. This feature is specific to AWS Bedrock models.')
    }
    
    if (textBlocks.length > 0) {
      request.messages.push({
        role: 'system',
        content: textBlocks.join('\n'),
      })
    }
  }
}

Files to Modify

  1. src/models/openai.ts: Update _formatRequest method (~20-30 lines changed)
  2. src/models/__tests__/openai.test.ts: Add 4 new test cases (~100-150 lines)

Acceptance Criteria

  • OpenAI model provider handles string systemPrompt (existing behavior maintained)
  • OpenAI model provider handles array systemPrompt with text blocks
  • OpenAI model provider handles array systemPrompt with cache points (with warning)
  • Console warning is emitted when cache points are present
  • Empty array systemPrompt is handled correctly (no system message added)
  • All existing tests continue to pass
  • New tests added for all array systemPrompt scenarios
  • Test coverage remains above 80%
  • Code follows existing patterns from Bedrock implementation
  • TypeScript compiles without errors
  • ESLint passes without errors
  • Prettier formatting is consistent

Backward Compatibility

  • All existing code using string systemPrompt continues to work unchanged
  • New array support is purely additive

Estimated Scope

  • Complexity: Low-Medium
  • Files Modified: 2 files
  • Lines Added/Modified: ~120-180 lines (including tests)
  • Time Estimate: 2-4 hours
  • Risk: Low (follows established patterns, backward compatible)

Related Context

  • Reference PR/Commit: Check recent commits that updated Bedrock for the SystemPrompt type change
  • Type Definition: src/types/messages.ts lines 162-186
  • Similar Feature: Reasoning block handling in OpenAI (lines 497-499, 581-584) shows pattern for unsupported features

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Language

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions