openai icon indicating copy to clipboard operation
openai copied to clipboard

chat with image_url bug

Open kobotis opened this issue 1 year ago • 12 comments

this is sample format from Openai Docs messages=[ { "role": "user", "content": [ {"type": "text", "text": "What’s in this image?"}, { "type": "image_url", "image_url": { "url": "https://placehold.co/600x400", }, }, ], } ], is sample must be {"type": "imageurl" , "image_url" : { "url": " "https://placehold.co/600x400"}

but dart_openai generates {"type":"image_url","image_url":"https://placehold.co/600x400"}

so OpenAI api says error Unhandled Exception: RequestFailedException(message: Invalid type for 'messages[2].content[1].image_url': expected an object, but got a string instead., statusCode: 400

dart_openai version is 5.1.0

kobotis avatar May 22 '24 01:05 kobotis

// ignore_for_file: public_member_api_docs, sort_constructors_first
/// {@template openai_chat_completion_choice_message_content_item_model}
/// This represents the content item of the [OpenAIChatCompletionChoiceMessageModel] model of the OpenAI API, which is used in the [OpenAIChat] methods.
/// {@endtemplate}
class OpenAIChatCompletionChoiceMessageContentItemModel {
  /// The type of the content item.
  final String type;

  /// The text content of the item.
  final String? text;

  /// The image url content of the item.
  final Map<String, String>? imageUrl;

  @override
  int get hashCode => type.hashCode ^ text.hashCode ^ imageUrl.hashCode;

  /// {@macro openai_chat_completion_choice_message_content_item_model}
  OpenAIChatCompletionChoiceMessageContentItemModel._({
    required this.type,
    this.text,
    this.imageUrl,
  });

  /// This is used to convert a [Map<String, dynamic>] object to a [OpenAIChatCompletionChoiceMessageContentItemModel] object.
  factory OpenAIChatCompletionChoiceMessageContentItemModel.fromMap(
    Map<String, dynamic> asMap,
  ) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: asMap['type'],
      text: asMap['text'],
      imageUrl: asMap['image_url'] != null ? {"url": asMap['image_url']} : null,
    );
  }

  /// Represents a text content item factory, which is used to create a text [OpenAIChatCompletionChoiceMessageContentItemModel].
  factory OpenAIChatCompletionChoiceMessageContentItemModel.text(String text) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: 'text',
      text: text,
    );
  }

  /// Represents a image content item factory, which is used to create a image [OpenAIChatCompletionChoiceMessageContentItemModel].
  factory OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl(
    String imageUrl,
  ) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: 'image_url',
      imageUrl: {"url": imageUrl},
    );
  }

  /// This method used to convert the [OpenAIChatCompletionChoiceMessageContentItemModel] to a [Map<String, dynamic>] object.
  Map<String, dynamic> toMap() {
    return {
      "type": type,
      if (text != null) "text": text,
      if (imageUrl != null) "image_url": imageUrl,
    };
  }

  @override
  bool operator ==(
    covariant OpenAIChatCompletionChoiceMessageContentItemModel other,
  ) {
    if (identical(this, other)) return true;

    return other.type == type &&
        other.text == text &&
        other.imageUrl == imageUrl;
  }

  @override
  String toString() => switch (type) {
        'text' =>
          'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, text: $text)',
        'image_url' =>
          'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, imageUrl: $imageUrl)',
        _ => 'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type)',
      };
}

i changed \lib\src\core\models\chat\sub_models\choices\sub_models\sub_models\content.dart and it works

kobotis avatar May 22 '24 01:05 kobotis

Any update under this issue?

hfranco346 avatar May 29 '24 04:05 hfranco346

The latest version 5.1.0 still has this issue. When is the next version expected to be updated?

https://pub.dev/packages/dart_openai/versions

reedmi avatar Jun 07 '24 06:06 reedmi

I have met this issue when I use model 'gpt-4o'.

aiviapp avatar Jun 07 '24 07:06 aiviapp

any update on this issue? Seems like the changes from pr 166 didn't make it to the released version

razyalov avatar Jun 09 '24 05:06 razyalov

please @anasfik release this to pub.dev

untitledappseu avatar Jun 11 '24 12:06 untitledappseu

For anyone needing encountering this problem, the temporary fix is to fork the package and then apply this fix Link. Has to be done sadly.

untitledappseu avatar Jun 11 '24 14:06 untitledappseu

For anyone needing encountering this problem, the temporary fix is to use to "gpt-4-vision-preview". Using that model it works for me.

This temprorary fix now not working for anyone who intersted in it. It return error 404 and error message "The model gpt-4-vision-preview has been deprecated, learn more here: https://platform.openai.com/docs/deprecations"

missionnowin avatar Jun 21 '24 00:06 missionnowin

For anyone needing encountering this problem, the temporary fix is to use to "gpt-4-vision-preview". Using that model it works for me.

This temprorary fix now not working for anyone who intersted in it. It return error 404 and error message "The model gpt-4-vision-preview has been deprecated, learn more here: https://platform.openai.com/docs/deprecations"

Updated my reply

untitledappseu avatar Jun 21 '24 14:06 untitledappseu

Is there any solution for this issue ?

bhavika-mamtora avatar Jul 18 '24 07:07 bhavika-mamtora

I migrated to https://pub.dev/packages/openai_dart

franboladoruiz avatar Jul 18 '24 09:07 franboladoruiz

How is this not fixed yet, the fix is simple? Also they deprecated the working model gpt-4-vision-preview so now there is no workaround. Will migrate to the package mentioned above

novalain avatar Aug 20 '24 20:08 novalain

Thanks to the community and you.

Resolved a while ago, please update to latest versions.

anasfik avatar Nov 08 '25 23:11 anasfik