linshenkx/prompt-optimizer · error · ImageError

INVALID_RESPONSE_FORMAT

INVALID_RESPONSE_FORMAT

Error message

INVALID_RESPONSE_FORMAT

What it means

parseImageResponse in the Ollama adapter requires the response JSON to have an array data field; otherwise it throws INVALID_RESPONSE_FORMAT. Ollama must mimic the OpenAI images response shape for the adapter to accept it.

Source

Thrown at packages/core/src/services/image/adapters/ollama.ts:187

      let errorMessage = `Ollama API error: ${response.status} ${response.statusText}`
      try {
        const errorData = await response.json()
        if (typeof errorData?.error?.message === 'string') {
          errorMessage = errorData.error.message
        }
      } catch {
        // ignore JSON parse errors
      }
      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, errorMessage)
    }

    const json = await response.json()
    return this.parseImageResponse(json, config)
  }

  private parseImageResponse(response: any, config: ImageModelConfig): ImageResult {
    if (!response?.data || !Array.isArray(response.data)) {
      throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)
    }

    const images = response.data.map((item: any) => {
      if (!item?.b64_json || typeof item.b64_json !== 'string') {
        throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)
      }
      const dataUrl = `data:image/png;base64,${item.b64_json}`
      return {
        b64: item.b64_json,
        mimeType: 'image/png',
        url: dataUrl
      }
    })

    return {
      images,
      metadata: {
        providerId: 'ollama',

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Log the raw Ollama response body to see its actual shape
  2. Ensure the base URL uses Ollama's OpenAI-compatible /v1/images/generations route
  3. Upgrade/downgrade Ollama to a version whose images API matches the adapter's expectation
Defensive patterns

Strategy: type-guard

Validate before calling

// ensure base URL targets the OpenAI-compat route
assert(config.baseUrl.match(/\/v1$/), 'Ollama base URL should end with /v1')

Type guard

function isOpenAiImagesShape(r: unknown): r is { data: unknown[] } {
  return !!r && Array.isArray((r as any).data)
}

Try / catch

try { ... } catch (e) {
  if (e instanceof ImageError && e.code === IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)
    // check base URL points at /v1, not native API
}

Prevention

When it happens

Trigger: Ollama returns a payload without data[] — native Ollama API format ({ models: ... } or generation format), an error object, or a non-images route responding.

Common situations: Pointing the adapter at Ollama's native API (11434 without the OpenAI-compat path), older Ollama versions with different response shapes, or mismatched endpoint paths.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/2187af4a84441a28. Report an issue: GitHub.