0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Difyカスタムツール作成における「invalid input syntax for type uuid」エラーの解決方法

Posted at

初めに

この記事では、Difyのカスタムツール作成時に発生した「invalid input syntax for type uuid」エラーの原因分析と解決策を解説します。実際のエラーログ、修正手順、テスト結果を交えながら、具体的な対応方法を説明します。

image.png


エラーの発生状況

エラーログの例

2025-03-15 17:09:28 db-1 | ERROR: invalid input syntax for type uuid: "" at character 745
2025-03-15 17:09:28 db-1 | STATEMENT: SELECT accounts.id ... WHERE accounts.id = ''::UUID
2025-03-15 17:09:28 api-1 | sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type uuid: ""

エラーの原因は、accounts.idのクエリパラメータとして空文字列が渡され、PostgreSQLがUUID形式に変換できなかったためです。特に、WHERE accounts.id = ''::UUIDの部分が問題の核心です。


解決策の実装(Pull Request内容)

対応内容

実装コードの例(Pull Requestより)

# api/models/tools.py
    @property
    def user(self) -> Account | None:
        if not self.user_id:
            return None
        return db.session.query(Account).filter(Account.id == self.user_id).first()

修正と修正後の確認

環境構築手順

# Difyリポジトリのクローン
git clone https://github.com/langgenius/dify.git dify-main
cd dify-main/docker

# .envファイルの作成
cp .env.example .env

# docker-composeの修正(ローカルビルド用)
vi docker-compose.yaml
---
services:
  api:
    build:
      context: ../api
      dockerfile: Dockerfile
  worker:
    build:
      context: ../api
      dockerfile: Dockerfile
---

# コンテナの起動
docker compose up -d

テスト結果

テスト結果のスクリーンショット
修正後はエラーが解消され、適切なバリデーションが動作


参考リンク:

この記事がDifyの開発者コミュニティに役立てば幸いです!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?