wavetermdev/waveterm · error

APIType must be '%s', got '%s'

Error message

APIType must be '%s', got '%s'

What it means

ConvertAIChatToUIChat only accepts AIChats whose APIType is APIType_OpenAIResponses; passing a chat created for a different backend (e.g. anthropic or openai-completions) fails this guard, reporting the expected and actual APIType.

Source

Thrown at pkg/aiusechat/openai/openai-convertmessage.go:553

		}
	} else if m.FunctionCallOutput != nil {
		// FunctionCallOutput messages are not converted to UIMessage
		return nil
	}
	if len(parts) == 0 {
		return nil
	}
	return &uctypes.UIMessage{
		ID:    m.MessageId,
		Role:  role,
		Parts: parts,
	}
}

// ConvertAIChatToUIChat converts an AIChat to a UIChat for OpenAI
func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {
	if aiChat.APIType != uctypes.APIType_OpenAIResponses {
		return nil, fmt.Errorf("APIType must be '%s', got '%s'", uctypes.APIType_OpenAIResponses, aiChat.APIType)
	}
	uiMessages := make([]uctypes.UIMessage, 0, len(aiChat.NativeMessages))
	for i, nativeMsg := range aiChat.NativeMessages {
		openaiMsg, ok := nativeMsg.(*OpenAIChatMessage)
		if !ok {
			return nil, fmt.Errorf("message %d: expected *OpenAIChatMessage, got %T", i, nativeMsg)
		}
		uiMsg := openaiMsg.convertToUIMessage()
		if uiMsg != nil {
			uiMessages = append(uiMessages, *uiMsg)
		}
	}
	return &uctypes.UIChat{
		ChatId:     aiChat.ChatId,
		APIType:    aiChat.APIType,
		Model:      aiChat.Model,
		APIVersion: aiChat.APIVersion,
		Messages:   uiMessages,

View on GitHub (pinned to a4447c1563)

Solutions

  1. Check aiChat.APIType before converting and route to the matching backend's ConvertAIChatToUIChat
  2. Re-create the chat with the OpenAI Responses backend if it was misconfigured
  3. Verify persisted chat metadata (APIType) was not corrupted or defaulted incorrectly

Example fix

// before
ui, err := openai.ConvertAIChatToUIChat(aiChat) // APIType is anthropic
// after
if aiChat.APIType != uctypes.APIType_OpenAIResponses {
    return fmt.Errorf("unsupported APIType %q for openai converter", aiChat.APIType)
}
ui, err := openai.ConvertAIChatToUIChat(aiChat)
Defensive patterns

Strategy: validation

Validate before calling

if aiChat.APIType != uctypes.APIType_OpenAIResponses {
    return fmt.Errorf("need %s chat, got %s", uctypes.APIType_OpenAIResponses, aiChat.APIType)
}

Type guard

func isOpenAIResponsesChat(c uctypes.AIChat) bool { return c.APIType == uctypes.APIType_OpenAIResponses }

Prevention

When it happens

Trigger: Calling ConvertAIChatToUIChat on an AIChat built/loaded with aiChat.APIType set to anything other than uctypes.APIType_OpenAIResponses.

Common situations: Mixing backends after switching providers; loading a persisted chat that was created with a different API type; wiring the OpenAI UI converter to a chat produced by the Anthropic backend.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/886f26daae999e7b. Report an issue: GitHub.