Skip to main content

dify-api-client

CI PyPI version Python 3.10+

This package is a fork of dify-client-python with custom modifications

It provides a convenient and powerful interface to interact with the Dify API, supporting both synchronous and asynchronous operations.

Main Features

  • Synchronous and Asynchronous Support: The client offers both synchronous and asynchronous methods, allowing for flexible integration into various Python codebases and frameworks.
  • Stream and Non-stream Support: Seamlessly work with both streaming and non-streaming endpoints of the Dify API for real-time and batch processing use cases.
  • Comprehensive Endpoint Coverage: Support completion, chat, workflows, feedback, file uploads, etc., the client covers all available Dify API endpoints.

Quick Start

Sync

Here's a quick example of how you can use the DifyClient to send a chat message.

import uuid
from dify_client import DifyClient, models

# Initialize the client with your API key
client = DifyClient(
    api_key="your-api-key",
    api_base="http://localhost/v1",
)
user = str(uuid.uuid4())

# Create a blocking chat request
blocking_chat_req = models.ChatRequest(
    query="Hi, dify-client-python!",
    inputs={"city": "Beijing"},
    user=user,
    response_mode=models.ResponseMode.BLOCKING,
)

# Send the chat message
chat_response = client.chat_messages(blocking_chat_req, timeout=60.)
print(chat_response)

# Create a streaming chat request
streaming_chat_req = models.ChatRequest(
    query="Hi, dify-client-python!",
    inputs={"city": "Beijing"},
    user=user,
    response_mode=models.ResponseMode.STREAMING,
)

# Send the chat message
for chunk in client.chat_messages(streaming_chat_req, timeout=60.):
    print(chunk)

Async

For asynchronous operations, use the AsyncDifyClient in a similar fashion:

import asyncio
import uuid

from dify_client import AsyncDifyClient, models
# Initialize the async client with your API key
async_client = AsyncDifyClient(
    api_key="your-api-key",
    api_base="http://localhost/v1",
)


# Define an asynchronous function to send a blocking chat message with BLOCKING ResponseMode
async def send_chat_message():
    user = str(uuid.uuid4())
    # Create a blocking chat request
    blocking_chat_req = models.ChatRequest(
        query="Hi, dify-client-python!",
        inputs={"city": "Beijing"},
        user=user,
        response_mode=models.ResponseMode.BLOCKING,
    )
    chat_response = await async_client.achat_messages(blocking_chat_req, timeout=60.)
    print(chat_response)


# Define an asynchronous function to send a chat message with STREAMING ResponseMode
async def send_chat_message_stream():
    user = str(uuid.uuid4())
    # Create a blocking chat request
    streaming_chat_req = models.ChatRequest(
        query="Hi, dify-client-python!",
        inputs={"city": "Beijing"},
        user=user,
        response_mode=models.ResponseMode.STREAMING,
    )
    async for chunk in await async_client.achat_messages(streaming_chat_req, timeout=60.):
        print(chunk)


# Run the asynchronous function
asyncio.gather(send_chat_message(), send_chat_message_stream())

Development

  • Setup env
uv sync --extra dev
  • Export python path to run dev
export PYTHONPATH=.

Metadata

Release files for dify-api-client 0.0.23

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dify-api-client 0.0.23
File Size Uploaded Provenance Actions
dify_api_client-0.0.23.tar.gz 102.9 kB Without provenance Details for dify_api_client-0.0.23.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dify-api-client 0.0.23
File Interpreter ABI Platform Actions
dify_api_client-0.0.23-py3-none-any.whl Python 3 none any Details for dify_api_client-0.0.23-py3-none-any.whl

Total release size: 144.4 kB

Release files / dify_api_client-0.0.23.tar.gz

Download URL dify_api_client-0.0.23.tar.gz
Size 102.9 kB
Tags Source
SHA-256 checksum
How to use checksums
097e33e2adaead6bcb8770a8b70dedfb4a2a40a55ea4011576532d150f1ced71
BLAKE2b-256 checksum
How to use checksums
815517f463f6bdcfa1f0c9be1214a63a7e3f8951998a01cb21f73f52536a46b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / dify_api_client-0.0.23-py3-none-any.whl

Download URL dify_api_client-0.0.23-py3-none-any.whl
Size 41.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8240f551a2a52e8366f9b7ef61f07b36859d754ea3bd18db8322bb4a4f55863
BLAKE2b-256 checksum
How to use checksums
15adc184ee397e97808e561426125c77ab7677a5701043307e2eb1ebc500316b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for dify-api-client 0.0.23

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page