Skip to content

feat: add multi-language API SDK (Python/Go/TypeScript)#18049

Open
Rander7 wants to merge 2 commits into
PaddlePaddle:mainfrom
Rander7:feature/api-sdk
Open

feat: add multi-language API SDK (Python/Go/TypeScript)#18049
Rander7 wants to merge 2 commits into
PaddlePaddle:mainfrom
Rander7:feature/api-sdk

Conversation

@Rander7
Copy link
Copy Markdown

@Rander7 Rander7 commented May 19, 2026

What problem does this PR solve?

PaddleOCR 下游集成目前使用同步 API 直调方式,缺少统一的多语言 SDK 封装。本 PR 新增 Python/Go/TypeScript 三语言 API SDK,将异步任务 API(提交→轮询→获取结果)封装为简单的阻塞式调用接口,用户调用一个方法即可直接拿到解析结果。

主要功能:

  • Python SDK:集成到 paddleocr/_api_client/,提供 APIClient(同步)+ AsyncAPIClient(异步)+ CLI 子命令 paddleocr api
  • Go SDK:api_sdk/go/,零依赖,Functional Options + context.Context 取消支持
  • TypeScript SDK:api_sdk/typescript/,零依赖,原生 fetch + AbortSignal 取消支持
  • 支持 4 种模型:PP-OCRv5、PP-StructureV3、PaddleOCR-VL、PaddleOCR-VL-1.5
  • 支持 URL 输入和本地文件上传两种模式
  • 指数退避轮询策略 + 完整错误类型层级

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update

Add Python/Go/TypeScript SDKs that wrap the PaddleOCR async job API
(submit → poll → fetch result) into simple blocking interfaces.

Python SDK:
- Integrated into paddleocr/_api_client/ as internal subpackage
- APIClient (sync) + AsyncAPIClient (asyncio) dual interfaces
- CLI subcommand: paddleocr api --model_type ocr/doc_parsing
- Supports PP-OCRv5, PP-StructureV3, PaddleOCR-VL, PaddleOCR-VL-1.5

Go SDK (api_sdk/go/):
- Zero dependencies (stdlib only)
- Functional options pattern + context.Context cancellation
- Operation object with Wait(ctx)/Poll(ctx) methods

TypeScript SDK (api_sdk/typescript/):
- Zero dependencies (native fetch, Node>=18)
- AbortSignal cancellation support
- ESM + CJS dual output

All SDKs share:
- Exponential backoff polling (3s initial, 1.5x, 15s max, configurable timeout)
- Typed error hierarchy (Auth/API/JobFailed/Timeout/Network/FileNotFound)
- URL and local file upload support
- Convenience methods + manual control (submit/wait/poll) interfaces
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 19, 2026

CLA assistant check
All committers have signed the CLA.

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented May 19, 2026

Thanks for your contribution!

@@ -0,0 +1,73 @@
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header应该用2026的

Comment thread paddleocr/_api_client/errors.py Outdated
# limitations under the License.


class PaddleOCRError(Exception):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑到这是在paddleocr主库中,叫这个名字是否有些范围太大。建议可以考虑 PaddleOCRAPIError 之类的

Comment thread paddleocr/_api_client/errors.py Outdated
"""Network connection failure."""


class FileNotFoundError(PaddleOCRError):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要评估是否更适合直接使用Python builtin的FileNotFoundError



@dataclass
class OCROptions:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里缺少了很多配置,建议参考官方文档补全:https://ai.baidu.com/ai-doc/AISTUDIO/Kmfl2ycs0

Comment thread paddleocr/_api_client/cli.py Outdated
"--model",
type=str,
default=None,
help="Model name (required for doc_parsing): PP-StructureV3, PaddleOCR-VL, PaddleOCR-VL-1.5",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里建议为未来预留,不限定死doc_parsing任务;以后OCR任务也可能会有PP-OCRv6等不同版本模型。

choices=["ocr", "doc_parsing"],
help="Task type: ocr or doc_parsing",
)
subparser.add_argument(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,我注意到底层定义了枚举,那么这里建议复用枚举值,而不是在字符串里把名字再硬编码一次

Comment thread paddleocr/_api_client/cli.py Outdated


def _resolve_model(model_str: str) -> Model:
mapping = {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然已经定义了枚举,这里不必再硬编码一次字符串

- Rename PaddleOCRError to PaddleOCRAPIError across all three SDKs
- Replace custom FileNotFoundError with Python builtin in Python SDK
- Expand OCROptions/DocParsingOptions with all API parameters (Python/Go/TS)
- CLI: use Model enum values instead of hardcoded strings
- CLI: allow --model for both ocr and doc_parsing tasks
- Update license header year to 2026
- Simplify TypeScript defaultPayload to empty object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants