Skip to main content

dify-client-python

dify-client-python is a typed Python SDK for Dify Runtime APIs, covering chat, completion, workflow, file upload, feedback, and audio conversion endpoints.

Requirements

  • Python >=3.8
  • Dify Runtime API (cloud or self-hosted) compatible with /v1 endpoints

Installation

pip install dify-client-python

What Is Supported

  • Sync and async clients: Client, AsyncClient
  • Blocking and streaming response modes
  • Chat and completion message APIs
  • Workflow run/stream/stop APIs
  • File upload APIs
  • Message feedback and suggestion APIs
  • Audio APIs:
    • audio-to-text
    • text-to-audio
  • Updated stream event support for newer workflow/chatflow runtimes:
    • workflow_paused, iteration_*, loop_*, text_chunk, text_replace
    • human_input_*, node_retry, agent_log, tts_message

Quick Start (Sync)

import uuid
from dify_client import Client, models

client = Client(
    api_key="your-api-key",
    api_base="https://api.dify.ai/v1",
)
user = str(uuid.uuid4())

req = models.ChatRequest(
    query="Hello from dify-client-python",
    inputs={},
    user=user,
    response_mode=models.ResponseMode.BLOCKING,
)

res = client.chat_messages(req, timeout=60.0)
print(res.answer)

Streaming Chat

stream_req = models.ChatRequest(
    query="Stream this answer",
    inputs={},
    user=user,
    response_mode=models.ResponseMode.STREAMING,
)

for event in client.chat_messages(stream_req, timeout=60.0):
    print(event.event, getattr(event, "answer", None))

Audio APIs

audio_text = client.audio_to_text(
    ("sample.wav", open("sample.wav", "rb"), "audio/wav"),
    models.AudioToTextRequest(user=user),
)
print(audio_text.text)

audio_bytes = client.text_to_audio(
    models.TextToAudioRequest(text="Hello world", user=user)
)
with open("speech.mp3", "wb") as f:
    f.write(audio_bytes)

Quick Start (Async)

import asyncio
from dify_client import AsyncClient, models

async_client = AsyncClient(api_key="your-api-key", api_base="https://api.dify.ai/v1")

async def main():
    req = models.ChatRequest(
        query="hello",
        inputs={},
        user="user-1",
        response_mode=models.ResponseMode.STREAMING,
    )
    async for chunk in await async_client.achat_messages(req, timeout=60.0):
        print(chunk.event)

asyncio.run(main())

Security Notes

  • Do not hardcode production API keys in source code.
  • Prefer environment variables or secret managers for api_key.
  • The SDK injects Authorization: Bearer ... headers, but does not log keys by default.
  • If you add your own logging middleware around requests, redact Authorization headers.

Development

python -m pip install -e . pytest pytest-cov flake8 build twine setuptools wheel
python -m flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
python -m pytest -q --cov=dify_client --cov-report=term-missing
python -m build --no-isolation
python -m twine check --strict dist/*

Release

Use RELEASE.md for the release checklist and commands.

Release files for dify-client-python 1.0.3

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-client-python 1.0.3
File Size Uploaded
dify_client_python-1.0.3.tar.gz 19.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dify-client-python 1.0.3
File Interpreter ABI Platform
dify_client_python-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 36.2 kB

Release files / dify_client_python-1.0.3.tar.gz

Download URL dify_client_python-1.0.3.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
14c44e5cfd9a3dac202499de166ad1fedf533f4cd2fd68cb94ff187efe5e91e5
BLAKE2b-256 checksum
How to use checksums
e473cabaa9afe4c4fb60ba482081612cd54ed98f6b49d34921af3e75180ec9ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / dify_client_python-1.0.3-py3-none-any.whl

Download URL dify_client_python-1.0.3-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99168b121859ffbf19dd2203d06e0a0646a83ac2d28c6f2c77a076309fa64681
BLAKE2b-256 checksum
How to use checksums
9af63683731ab73add636a4c841230cc50c7d16ff8ee0e2eb5ba262284a047a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

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