SymPy
Simplify, factor, differentiate, integrate and solve symbolic math expressions
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/sympy/index.md to help me query SymPy with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
Factor polynomials and find their roots
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly
| poly | factored | roots |
|---|---|---|
| x**2 - 2 | x**2 - 2 | ["-sqrt(2)", "sqrt(2)"] |
| x**3 - 6*x**2 + 11*x - 6 | (x - 3)*(x - 2)*(x - 1) | ["1", "2", "3"] |
Quick start
Choose where to run SymPy, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'sympy' AS sympy (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-sympy-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'sympy' AS sympy (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-sympy:0.5.0'
);
3. Run a query
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'sympy' AS sympy
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly
Query Farm operates SymPy for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use sympy as the catalog alias for the query below.
4. Run a query
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'sympy' AS sympy (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-sympy-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'sympy' AS sympy (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-sympy:0.5.0'
);
Overview
Symbolic math (CAS): simplify/solve/differentiate/integrate/factor for SQL via SymPy.

Do computer algebra in SQL — simplify, expand, factor, differentiate, integrate, and solve symbolic expressions stored as text columns, powered by the SymPy computer algebra system and exposed as DuckDB scalar functions.
What it does
This VGI worker turns DuckDB into a symbolic calculator that works with symbols, not just numbers. Instead of shelling out to Python notebooks or a separate CAS, you can run algebra and calculus directly inside a query — manipulating formulas held in VARCHAR columns. It is built for data engineers, scientists, educators, and anyone who needs to normalize, transform, or verify mathematical expressions at scale alongside the rest of their SQL pipeline. Every function returns canonical, deterministic output that is stable across runs, so results are safe to materialize, diff, and join.
How it works
The catalog wraps SymPy — the pure-Python, BSD-licensed computer algebra library (documentation, source) — behind a set of per-row Arrow scalar functions. Expression strings are never passed to eval or sympify: each one runs through a hardened allow-list parser (a tokenize-based screen plus a restricted namespace with empty __builtins__), so a malformed or hostile string is rejected or returns NULL, never executed. SymPy itself produces a canonical string form for results, which is why outputs are reproducible and worth pinning in tests.
When to use it
Reach for this catalog whenever a value in your data is a formula rather than a number and you need to reason about it: canonicalize free-form formula columns so equivalent inputs collapse to one representation, grade student or model answers against a key, derive derivatives and antiderivatives for a scientific pipeline, solve equations row by row, or render expressions for display. Because every transform is a per-row scalar that yields NULL on invalid or unsafe input (rather than aborting the scan), it drops into an ordinary projection or predicate and composes over messy, semi-structured text without special-casing.
SELECT sympy.main.simplify('sin(x)**2 + cos(x)**2'); -- '1'
SELECT sympy.main.differentiate('x**3', 'x'); -- '3*x**2'Notes
Invalid or unsafe expressions yield NULL rather than erroring (except evaluate, which raises on a structurally wrong vars_json), so the transforms compose cleanly over messy columns. Parsing is sandboxed: hostile strings are rejected before any evaluation happens.
Catalog
Everything sympy exposes once attached — read straight from the worker's
released build.
main 10 functions
Symbolic math (CAS): simplify/solve/differentiate/integrate/factor for SQL
Schema examples (12)
Simplify a Pythagorean trig identity to 1.
SELECT sympy.main.simplify('sin(x)**2 + cos(x)**2');
Expand a squared binomial into a flat polynomial.
SELECT sympy.main.expand('(x + 1)**2');
Factor a difference of squares into a product.
SELECT sympy.main.factor('x**2 - 1');
Differentiate x**3 with respect to x.
SELECT sympy.main.differentiate('x**3', 'x');
8 more examples
Integrate 2*x with respect to x (no constant of integration).
SELECT sympy.main.integrate('2*x', 'x');
Solve a quadratic equation for x, returning a VARCHAR[] of roots.
SELECT sympy.main.solve('x**2 - 4', 'x');
Substitute x=3, y=1 into an expression and evaluate to a DOUBLE.
SELECT sympy.main.evaluate('x**2 + y', '{"x":3,"y":1}');
Test that two different-looking expressions are symbolically equal.
SELECT sympy.main.symbolic_equal('2*(x+1)', '2*x+2');
Find the stationary points of x**3 - 3*x by solving its derivative for zero, then evaluate the function at each one: a local maximum of 2.0 at x = -1 and a local minimum of -2.0 at x = 1.
WITH crit AS (
SELECT UNNEST(sympy.main.solve(sympy.main.differentiate('x**3 - 3*x', 'x'), 'x')) AS root
), pts AS (
SELECT root, sympy.main.evaluate(root, '{}') AS x_value FROM crit
)
SELECT root, x_value,
sympy.main.evaluate('x**3 - 3*x', '{"x": ' || x_value || '}') AS f_value
FROM pts
ORDER BY x_value;
Integrate each integrand, then verify the antiderivative by differentiating it back and testing symbolic equality with the original (all four rows are verified TRUE, even where the round-tripped string looks different).
SELECT integrand,
sympy.main.integrate(integrand, 'x') AS antiderivative,
sympy.main.symbolic_equal(
sympy.main.differentiate(sympy.main.integrate(integrand, 'x'), 'x'), integrand) AS verified
FROM (VALUES ('x*exp(x)'), ('1/(1 + x**2)'), ('sin(x)**2'), ('log(x)')) AS t(integrand)
ORDER BY integrand;
Grade free-form answers against the key (x + 1)**2: render each answer as LaTeX for display and mark it correct when it is symbolically equal to the key (only x**2 + 1 is wrong).
SELECT answer,
sympy.main.to_latex(answer) AS latex,
sympy.main.symbolic_equal(answer, '(x + 1)**2') AS correct
FROM (VALUES ('x**2 + 2*x + 1'), ('(1 + x)*(x + 1)'), ('x**2 + 1'), ('x*(x + 2) + 1')) AS t(answer)
ORDER BY answer;
Show each polynomial's factorization over the rationals beside its roots: x**3 - 6*x**2 + 11*x - 6 factors into (x - 3)*(x - 2)*(x - 1) with roots 1, 2, 3, while x**2 - 2 comes back unfactored because its roots -sqrt(2) and sqrt(2) are irrational.
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly;
| poly | factored | roots |
|---|---|---|
| x**2 - 2 | x**2 - 2 | ["-sqrt(2)", "sqrt(2)"] |
| x**3 - 6*x**2 + 11*x - 6 | (x - 3)*(x - 2)*(x - 1) | ["1", "2", "3"] |
differentiate (expr VARCHAR, var VARCHAR)
Derivative of an expression with respect to a variable, or NULL if invalid
Differentiate x**3 with respect to x.
SELECT sympy.main.differentiate('x**3', 'x');
Differentiate sin(x) with respect to x.
SELECT sympy.main.differentiate('sin(x)', 'x');
Find the stationary points of x**3 - 3*x by solving its derivative for zero, then evaluate the function at each one: a local maximum of 2.0 at x = -1 and a local minimum of -2.0 at x = 1.
WITH crit AS (
SELECT UNNEST(sympy.main.solve(sympy.main.differentiate('x**3 - 3*x', 'x'), 'x')) AS root
), pts AS (
SELECT root, sympy.main.evaluate(root, '{}') AS x_value FROM crit
)
SELECT root, x_value,
sympy.main.evaluate('x**3 - 3*x', '{"x": ' || x_value || '}') AS f_value
FROM pts
ORDER BY x_value;
evaluate (expr VARCHAR, vars_json VARCHAR)
Substitute numeric values from a JSON object like '{"x":2,"y":3}' and evaluate to a DOUBLE. NULL if invalid, non-numeric, or free symbols remain.
Substitute x=3, y=1 into an expression and evaluate to a DOUBLE.
SELECT sympy.main.evaluate('x**2 + y', '{"x":3,"y":1}');
Evaluate a constant expression (2*pi) with no variables.
SELECT sympy.main.evaluate('2*pi', '{}');
expand (expr VARCHAR)
Algebraically expanded form of an expression, or NULL if invalid/unsafe
Expand a squared binomial into a flat polynomial.
SELECT sympy.main.expand('(x + 1)**2');
Expand a product of conjugates to a difference of squares.
SELECT sympy.main.expand('(x + y)*(x - y)');
factor (expr VARCHAR)
Factored form of an expression, e.g. '(x - 1)*(x + 1)', or NULL if invalid
Factor a difference of squares into a product.
SELECT sympy.main.factor('x**2 - 1');
Factor a perfect-square trinomial.
SELECT sympy.main.factor('x**2 + 2*x + 1');
Show each polynomial's factorization over the rationals beside its roots: x**3 - 6*x**2 + 11*x - 6 factors into (x - 3)*(x - 2)*(x - 1) with roots 1, 2, 3, while x**2 - 2 comes back unfactored because its roots -sqrt(2) and sqrt(2) are irrational.
SELECT poly,
sympy.main.factor(poly) AS factored,
sympy.main.solve(poly, 'x') AS roots
FROM (VALUES ('x**3 - 6*x**2 + 11*x - 6'), ('x**2 - 2')) AS t(poly)
ORDER BY poly;
| poly | factored | roots |
|---|---|---|
| x**2 - 2 | x**2 - 2 | ["-sqrt(2)", "sqrt(2)"] |
| x**3 - 6*x**2 + 11*x - 6 | (x - 3)*(x - 2)*(x - 1) | ["1", "2", "3"] |
integrate (expr VARCHAR, var VARCHAR)
Indefinite integral of an expression w.r.t. a variable (no +C), or NULL if invalid or no closed form is found
Integrate 2*x with respect to x (no constant of integration).
SELECT sympy.main.integrate('2*x', 'x');
Integrate cos(x) with respect to x.
SELECT sympy.main.integrate('cos(x)', 'x');
Integrate each integrand, then verify the antiderivative by differentiating it back and testing symbolic equality with the original (all four rows are verified TRUE, even where the round-tripped string looks different).
SELECT integrand,
sympy.main.integrate(integrand, 'x') AS antiderivative,
sympy.main.symbolic_equal(
sympy.main.differentiate(sympy.main.integrate(integrand, 'x'), 'x'), integrand) AS verified
FROM (VALUES ('x*exp(x)'), ('1/(1 + x**2)'), ('sin(x)**2'), ('log(x)')) AS t(integrand)
ORDER BY integrand;
simplify (expr VARCHAR)
Algebraically simplified form of an expression, or NULL if invalid/unsafe
Simplify a Pythagorean trig identity to 1.
SELECT sympy.main.simplify('sin(x)**2 + cos(x)**2');
Cancel a common factor from a rational expression.
SELECT sympy.main.simplify('(x**2 - 1)/(x - 1)');
solve (equation VARCHAR, var VARCHAR)
Solve an equation for a variable, returning solutions as VARCHAR[]. Accepts 'lhs = rhs' or a bare expression assumed equal to zero. NULL if invalid; empty list if no solutions.
Solve a quadratic for x, returning a VARCHAR[] of roots.
SELECT sympy.main.solve('x**2 - 4', 'x');
Solve a linear 'lhs = rhs' equation and unnest the solution rows.
SELECT UNNEST(sympy.main.solve('2*x = 10', 'x'));
symbolic_equal (a VARCHAR, b VARCHAR)
True if two expressions are symbolically equivalent (simplify(a-b)==0), or NULL if either is invalid
Prove two different-looking forms are equal (TRUE).
SELECT sympy.main.symbolic_equal('2*(x+1)', '2*x+2');
Show two distinct functions are not equal (FALSE).
SELECT sympy.main.symbolic_equal('sin(x)', 'cos(x)');
Grade free-form answers against the key (x + 1)**2: render each answer as LaTeX for display and mark it correct when it is symbolically equal to the key (only x**2 + 1 is wrong).
SELECT answer,
sympy.main.to_latex(answer) AS latex,
sympy.main.symbolic_equal(answer, '(x + 1)**2') AS correct
FROM (VALUES ('x**2 + 2*x + 1'), ('(1 + x)*(x + 1)'), ('x**2 + 1'), ('x*(x + 2) + 1')) AS t(answer)
ORDER BY answer;
sympy_version (n BIGINT)
The SymPy version string backing this worker
Report the backing SymPy version string.
SELECT sympy.main.sympy_version(1);
Check that the backing SymPy version is present (non-empty).
SELECT length(sympy.main.sympy_version(1)) > 0;
to_latex (expr VARCHAR)
LaTeX rendering of an expression, or NULL if invalid/unsafe
Render x squared as LaTeX markup.
SELECT sympy.main.to_latex('x**2');
Render a square root as LaTeX markup.
SELECT sympy.main.to_latex('sqrt(x)');
Try these queries
Simplify a Pythagorean trig identity to 1.
SELECT sympy.main.simplify('sin(x)**2 + cos(x)**2')
Uses simplify
Expand a squared binomial into a flat polynomial.
SELECT sympy.main.expand('(x + 1)**2')
Uses expand
Differentiate x**3 with respect to x.
SELECT sympy.main.differentiate('x**3', 'x')
Uses differentiate
Integrate 2*x with respect to x (no constant of integration).
SELECT sympy.main.integrate('2*x', 'x')
Uses integrate
Solve a quadratic equation for x, returning a VARCHAR[] of roots.
SELECT sympy.main.solve('x**2 - 4', 'x')
Uses solve
17 more examples in the Catalog, each listed under the table or function it uses.
Questions it can answer
Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.
- Algebraically simplify the expression sin(x)**2 + cos(x)**2 to its simplest canonical form.
- Expand the expression (x + 1)**2 by multiplying it out into a flat polynomial.
- Factor the polynomial x**2 - 1 into a product of irreducible factors.
- Render the expression x**2 as a LaTeX markup string.
- Compute the symbolic derivative of x**3 with respect to x.
- Compute the indefinite integral (antiderivative, with no constant of integration) of 2*x with respect to x.
SymPy is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
Anomaly
Statistics & ML
Find outliers, discords, repeated motifs and change points in numeric series from SQL
Causal
Statistics & ML
Estimate treatment effects (ATE, ATT) and propensity scores from observational data with IPW, regression adjustment, and AIPW
Explain
Statistics & ML
Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance