pytidb
Original:🇺🇸 English
Translated
14 scriptsChecked / no sensitive code detected
PyTiDB (pytidb) setup and usage for TiDB from Python. Covers connecting, table modeling (TableModel), CRUD, raw SQL, transactions, vector/full-text/hybrid search, auto-embedding, custom embedding functions, and reference templates/snippets (vector/hybrid/image) plus agent-oriented examples (RAG/memory/text2sql).
4installs
Sourcepingcap/agent-rules
Added on
NPX Install
npx skill4agent add pingcap/agent-rules pytidbTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →PyTiDB (pytidb)
Use this skill to connect to TiDB from Python via , define tables, and build search / AI features on top.
pytidbWhen to Use This Skill
- You want a Python ORM-like experience on TiDB via (built on SQLAlchemy).
pytidb - You want vector search / full-text search / hybrid search on TiDB with high-level APIs.
- You want runnable starter templates (scripts + small examples) you can adapt.
Need to provision a TiDB Cloud cluster first? Use (TiDB X) for cluster lifecycle guidance.
tidbxCode Generation Rules (Python)
- Never hardcode credentials; use env vars () and document required variables.
.env - Prefer and pinned deps for reproducibility.
python -m venv .venv - When editing requirements.txt, do not invent pytidb versions, use an unpinned pytidb by default unless the user explicitly requests it and the version has been verified to exist.
- Keep examples minimal and runnable; avoid framework-specific assumptions unless the user asks.
- Use parameterized SQL for any dynamic value (SQL injection safety).
- For interactive environments, avoid “table already defined” errors (use /
extend_existing/open_tablepatterns).if rows()==0
Available Guides
Each guide is a self-contained walkthrough with a checklist and phases:
- — one-file “connect → create table → insert → vector search”
guides/quickstart.md - — vector / full-text / hybrid: when to use which, plus gotchas
guides/search.md - — examples playbook (vector/hybrid/image)
guides/demos.md - — agent-ish examples (RAG / memory / text2sql)
guides/agent-apps.md - — connection, TLS, embedding, and index/search issues
guides/troubleshooting.md - — implement a custom embedding function (example: BGE-M3)
guides/custom-embedding.md
I’ll infer your intent (CRUD vs search vs “agent app”), then point you to the smallest guide and template set that gets you running.
Templates & Scripts
Each template is a complete file you can copy into your project. Choose the smallest one that matches your goal.
Core usage
- — minimal end-to-end: connect → create table → insert → vector search
templates/quickstart.py - — basic table modeling + CRUD lifecycle (create/truncate/insert/query/update/delete)
templates/crud.py - — auto embedding with pluggable providers (env-driven)
templates/auto_embedding.py - — vector search example (optional metadata filter + threshold)
templates/vector_search.py - — hybrid search example (FullTextField + vector field) with fused scoring
templates/hybrid_search.py
Image search
- — image-to-image or text-to-image search (requires multimodal embedding + Pillow)
templates/image_search.py - — loads Oxford Pets dataset into TiDB (used by
templates/image_search_data_loader.py)image_search.py
Custom embeddings
- — example
templates/custom_embedding_function.pyimplementation (BGE-M3 via FlagEmbedding)BaseEmbeddingFunction - — uses the custom embedder with auto embedding + vector search
templates/custom_embedding.py
Agent-ish examples
- — minimal RAG: retrieve via vector search, then generate via local LLM (Ollama via LiteLLM)
templates/rag.py - — reusable “memory” library (extract facts → store → retrieve)
templates/memory_lib.py - — CLI memory chat example using
templates/memory.pymemory_lib.py - — interactive Text2SQL (generates SQL via OpenAI; asks before executing)
templates/text2sql.py
Scripts
- — quick connection +
scripts/validate_connection.pysmoke test (supports params orSELECT 1)DATABASE_URL
Related Skills
- — provision/manage TiDB Cloud (TiDB X) clusters
tidbx
Workflow
I will:
- Confirm your TiDB deployment (Cloud Starter vs self-managed) and how you want to connect (params vs ).
DATABASE_URL - Help you set env vars, validate the connection, and choose the right path:
- CRUD/table modeling
- vector/full-text/hybrid search (and embedding provider)
- example templates
- Generate the minimal set of files and commands to get you running.