Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness Python SDK Usage

DeepSeek Harness Python SDK Usage

The Web UI is for people to watch, but when you need to call dsh from a program, you need the SDK

This chapter introduces the Python SDK: how to install it, how to run the repo's built-in examples, and how to call it in your own programs


What problem does the Python SDK solve

The SDK turns dsh into a one-line call in your program, rather than a page in a browser

DeepSeekHarness is the core class of the Python SDK, using a context manager to manage the runtime's lifecycle

Entering the with block lazily starts the built-in runtime, exiting releases it automatically, and run can be called repeatedly in between

Suitable scenarios: batch tasks, integrating dsh into your own products, driving the Agent in tests

After the SDK is installed, the runtime doesn't need the system to provide Node.js; the Python process brings its own copy


Prerequisites

The SDK has clear system requirements; confirm against them first

Depends on Requirement
Python 3.10 or higher
Git Installed
Operating system Linux x64, Linux arm64, macOS 14+ (Apple silicon), Windows x64
API endpoint DeepSeek-compatible API endpoint and credentials
Workspace An isolated workspace the agent can modify and an isolated dsh home

Install the SDK

Clone the repo to get runnable examples, create a virtual environment, then install the SDK and the built-in runtime of the same version



$ git clone https://github.com/deepseek-ai/deepseek-harness.git

$ cd deepseek-harness

$ python -m venv .venv

$ . .venv/bin/activate

$ python -m pip install deepseek-harness-sdk

Using Windows PowerShell py -3.10 -m venv .venv Create the environment, then use .venv\Scripts\Activate.ps1 to activate

A virtual environment isolates the SDK from other system Python packages, which is the officially recommended approach

The installed runtime doesn't need the system to provide Node.js

The examples and SDK never silently read ~/.dsh; you must pass the home path explicitly


Run the repo's built-in example

The repo includes a minimal.py example, located at python/sdk/examples/ directory; getting it to run verifies the entire SDK chain

First set the credentials in the environment



$ export DEEPSEEK_API_KEY=sk-your-key-here

# export DEEPSEEK_BASE_URL=http://127.0.0.1:8000/v1

# export DSH_MODEL=deepseek-v4-flash

# export DSH_SYSTEM_PROMPT='You are a helpful software engineer assistant.'

If the model is provided not by the default DeepSeek endpoint but through an OpenAI-compatible proxy, you also need to set DEEPSEEK_BASE_URL

Then run a task against the isolated workspace and dsh home



$ python python/sdk/examples/minimal.py \

  --workspace /absolute/path/to/disposable-workspace \

  --dsh-home /absolute/path/to/example-dsh-home \

  --session-id example-001 \

  "Inspect the repository and fix the failing tests."

The script prints the assistant's final reply

Under the specified home, the generated sdk-minimal profile, installed plugins, and uncompressed JSONL session logs under the sessions/ directory are saved


Using the SDK in your own programs

The repo's built-in example is actually a lightweight wrapper around the SDK call below; the core is just two steps

Example

File path: equivalent of python/sdk/examples/minimal.py

from

pathlib

import

Path

from

deepseek_harness

import

DeepSeekHarness

The workspace the agent can access, must be an absolute path

workspace

=

Path

(

"/absolute/path/to/disposable-workspace"

)

.

resolve

(

)

Isolated dsh home: stores profiles, plugins and session logs, must be an absolute path

dsh_home

=

Path

(

"/absolute/path/to/example-dsh-home"

)

.

resolve

(

)

Context manager: lazily starts the built-in runtime on entry, releases automatically on exit

with

DeepSeekHarness

(

    provider

=

"deepseek-official"

,

 

Use the official DeepSeek provider

    model

=

"deepseek-v4-flash"

,

     

Model name; the SDK defaults to this too

    max_tokens

=

49

_152

,

             

Maximum number of tokens for a single reply

    cwd

=

str

(

workspace

)

,

           

Set the workspace as the agent's working directory

    dsh_home

=

str

(

dsh_home

)

,

       

Isolated dsh home

   

profile

=

"sdk-minimal"

,

         

The included minimal profile

)

as

harness:

   

Send a task; session_id identifies this persistent conversation

    result

=

harness.

run

(

       

"Inspect the repository and fix the failing tests."

,

        session_id

=

"example-001"

,

   

)

Print the assistant's final reply

print

(

result.

final_response

)

DeepSeekHarness lazily starts the built-in dsh --profile sdk-minimal process and reuses it continuously until the context manager is exited

The same harness can call run multiple times in a with block; the runtime starts only once

The profile, its persistent patch, the home patch and the patches parameter passed in together form the application config; to switch to a custom profile, the config must contain a JSON-RPC server plugin (such as @deepseek-ai/dsh-sdk-app), otherwise startup fails


Reusing a session id: preserving the Bash process

This is the SDK's most easily overlooked and most useful rule

Reusing the same harness and session id preserves the Bash process owned by that session

Including its working directory, exported variables and shell functions, all carry over to the next call

Example

First call: export a variable in runoob-workspace

result

=

harness.

run

(

   

"Run cd /repo && export RUNOOB_MODE=dev and confirm."

,

    session_id

=

"runoob-session"

,

)

print

(

result.

final_response

)

Second call: reuse the same session id, and that exported variable is still there

result

=

harness.

run

(

   

"Print the value of RUNOOB_MODE."

,

    session_id

=

"runoob-session"

,

)

print

(

result.

final_response

)

Independent tasks should use a new session id; only reuse the original id when the next call needs to continue the same persistent conversation


Overview of the example bundle

The sdk-minimal bundle behind minimal.py is a deliberately trimmed config; let's see what it does item by item

property Value
System prompt DSH_SYSTEM_PROMPT; when unset, uses You are a helpful software engineer assistant.
The model used by minimal.py --model, then DSH_MODEL, then deepseek-v4-flash
Model-facing tool Persistent bash (pwsh on Windows), read_image and working_directory
Bash timeout 300 seconds
Context compression Auto-execute: when a threshold is reached, generate a summary of earlier history via an extra model request
Persistent checkpoint Enabled: wait for history to flush to disk before model requests, ensuring consistent recovery after a crash
Session persistence Uncompressed JSONL under /sessions
Permission Fixed to danger-full-access; can only run in a disposable environment

The bundle's only package inserts the complete config tree over an empty root,does not include dsh-base, so tools added to the base profile in the future won't appear implicitly

It omits plugins such as harness identity, workspace prompt text, skill, task tools, subagent, Web tools and local instruction discovery

The overall call flow is as follows

Python SDK call flow diagram


Explicitly enable str_replace_editor

The included runtime contains str_replace_editor, but sdk-minimal's default config treedoes not mount it。

When you need a file editing tool, save the following config as editor.patch.yml:

Example

File path: editor.patch.yml

insert adds the editor tool and reuses the existing filesystem backend

:

    - id

:

tool-str-replace-editor

      name

:

'@deepseek-ai/dsh-tool-str-replace-editor'

Pass the patches parameter when constructing DeepSeekHarness to take effect for a single run:

Example

with

DeepSeekHarness

(

   

profile

=

"sdk-minimal"

,

    patches

=

(

"/absolute/path/to/editor.patch.yml"

,

)

,

 

Patch appended for a single startup

   

...other parameters same as above

)

as

harness:

    ...

To make it persistent, write this patch into $DSH_HOME/profiles/sdk-minimal/cordis.patch.yml. This patch also applies to the standard sdk profile

Plugin management uses the dsh plugin command: first set DSH_HOME and run once dsh --profile sdk-minimal --dump-default-config to initialize the profile, then use dsh plugin --profile sdk-minimal add file:/path/to/my-plugin-bundle to install - only management commands need pnpm; starting the SDK itself doesn't


Session log upload and disabling

sdk-minimal comes with a session log contributor by default: each DeepSeek request uploads unsettled events along with the request, up to maxBytesat a time (default 8 MiB)

It's used for platform-side diagnostics, isn't within messages, system prompts or tool schemas, and doesn't add model input tokens. It still involves sending session data, so before use you should decide whether to disable it according to your own data requirements

If you don't want uploads, disable it in the profile patch:

Example

File path: $DSH_HOME/profiles/sdk-minimal/cordis.patch.yml

Override this config line by id to disable session log upload

:

session-log-deepseek

  config

:

    enabled

:

false


The boundaries of danger-full-access

This example bundle uses the danger-full-access permission preset fixed; you must understand its boundaries

danger-full-access can only run in a disposable checkout or container

Bash and file access can modify any path the runtime process has access to, with no sandbox fallback

On Windows the persistent shell uses pwsh, likewise without any path restrictions

In other words, before using it on a real project, make sure this environment is one you wouldn't mind breaking


Summary and self-test

In one sentence: the Python SDK wraps the runtime in a DeepSeekHarness context manager and sends tasks with run(prompt, session_id)

Test your understanding with three small questions

  1. What does reusing the same session id preserve?
  2. When should you use a new session id?
  3. Which tools does sdk-minimal open to the model by default?

Reference answer: it preserves the session's Bash process, including working directory, exported variables and shell functions

Use a new id for independent tasks; only reuse when you need to continue the same persistent conversation

Persistent bash (pwsh on Windows), read_image and working_directory; str_replace_editor needs an explicit patch to enable