langchain-onlinecompiler 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_onlinecompiler-0.1.0/.gitignore +9 -0
- langchain_onlinecompiler-0.1.0/LICENSE +21 -0
- langchain_onlinecompiler-0.1.0/Makefile +23 -0
- langchain_onlinecompiler-0.1.0/PKG-INFO +220 -0
- langchain_onlinecompiler-0.1.0/README.md +191 -0
- langchain_onlinecompiler-0.1.0/docs/onlinecompiler.mdx +187 -0
- langchain_onlinecompiler-0.1.0/langchain_onlinecompiler/__init__.py +29 -0
- langchain_onlinecompiler-0.1.0/langchain_onlinecompiler/py.typed +0 -0
- langchain_onlinecompiler-0.1.0/langchain_onlinecompiler/toolkits.py +66 -0
- langchain_onlinecompiler-0.1.0/langchain_onlinecompiler/tools.py +493 -0
- langchain_onlinecompiler-0.1.0/pyproject.toml +94 -0
- langchain_onlinecompiler-0.1.0/tests/__init__.py +0 -0
- langchain_onlinecompiler-0.1.0/tests/integration_tests/__init__.py +0 -0
- langchain_onlinecompiler-0.1.0/tests/integration_tests/test_compile.py +6 -0
- langchain_onlinecompiler-0.1.0/tests/integration_tests/test_tools.py +121 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/__init__.py +0 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/conftest.py +163 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/test_agent.py +86 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/test_imports.py +18 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/test_standard.py +55 -0
- langchain_onlinecompiler-0.1.0/tests/unit_tests/test_tools.py +432 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RunCode Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
.PHONY: test integration_tests lint format type build
|
|
2
|
+
|
|
3
|
+
# Unit tests talk only to a fake API on 127.0.0.1.
|
|
4
|
+
test:
|
|
5
|
+
pytest tests/unit_tests --allow-hosts=127.0.0.1
|
|
6
|
+
|
|
7
|
+
# Live tests: they need ONLINECOMPILER_API_KEY and use about 20 runs of its allowance.
|
|
8
|
+
integration_tests:
|
|
9
|
+
pytest -v tests/integration_tests
|
|
10
|
+
|
|
11
|
+
lint:
|
|
12
|
+
ruff check .
|
|
13
|
+
ruff format --check .
|
|
14
|
+
|
|
15
|
+
format:
|
|
16
|
+
ruff format .
|
|
17
|
+
ruff check --fix .
|
|
18
|
+
|
|
19
|
+
type:
|
|
20
|
+
mypy langchain_onlinecompiler tests
|
|
21
|
+
|
|
22
|
+
build:
|
|
23
|
+
python -m build
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: langchain-onlinecompiler
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangChain tools for OnlineCompiler.io: let an agent run code, or judge code against test cases, in 46 languages in a sandbox
|
|
5
|
+
Project-URL: Homepage, https://onlinecompiler.io
|
|
6
|
+
Project-URL: Documentation, https://github.com/onlinecompiler-io/langchain-onlinecompiler/tree/main/python#readme
|
|
7
|
+
Project-URL: Source, https://github.com/onlinecompiler-io/langchain-onlinecompiler
|
|
8
|
+
Project-URL: Issues, https://github.com/onlinecompiler-io/langchain-onlinecompiler/issues
|
|
9
|
+
Project-URL: Changelog, https://onlinecompiler.io/changelog
|
|
10
|
+
Author: RunCode Inc.
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: agents,code execution,code interpreter,langchain,langgraph,online compiler,online judge,onlinecompiler,sandbox,tools
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: <4.0,>=3.11
|
|
26
|
+
Requires-Dist: langchain-core<2.0.0,>=1.0.0
|
|
27
|
+
Requires-Dist: onlinecompiler<1.0.0,>=0.9.0
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# langchain-onlinecompiler
|
|
31
|
+
|
|
32
|
+
LangChain tools for [OnlineCompiler.io](https://onlinecompiler.io): let an agent run code, or judge code against test cases, in 46 languages. Each run gets a fresh sandbox with no network access.
|
|
33
|
+
|
|
34
|
+
- `OnlineCompilerRunCode` (tool name `onlinecompiler_run_code`) runs a program and returns its output, errors, exit code, time and memory.
|
|
35
|
+
- `OnlineCompilerRunTests` (tool name `onlinecompiler_run_tests`) judges a program against test cases and returns an overall verdict, a score from 0 to 100 and a verdict per test.
|
|
36
|
+
- `OnlineCompilerToolkit` gives you both.
|
|
37
|
+
|
|
38
|
+
The tools call the OnlineCompiler.io REST API through the official [`onlinecompiler`](https://pypi.org/project/onlinecompiler/) Python client.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install -U langchain-onlinecompiler
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Get an API key
|
|
47
|
+
|
|
48
|
+
Create a free key at [api.onlinecompiler.io/compilers/api-keys/](https://api.onlinecompiler.io/compilers/api-keys/) (key type **API**). The free plan includes 100,000 runs a month. Then set it in the environment:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
export ONLINECOMPILER_API_KEY="your-api-key"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
or pass it to a tool or the toolkit as `api_key="..."`. The key is held as a pydantic `SecretStr`, so it does not show up in reprs or logs.
|
|
55
|
+
|
|
56
|
+
## Use the tools directly
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from langchain_onlinecompiler import OnlineCompilerRunCode, OnlineCompilerRunTests
|
|
60
|
+
|
|
61
|
+
run_code = OnlineCompilerRunCode()
|
|
62
|
+
print(run_code.invoke({"compiler": "python-3.14", "code": "print(2 ** 100)"}))
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```text
|
|
66
|
+
status: success
|
|
67
|
+
exit_code: 0
|
|
68
|
+
time: 0.0309 s
|
|
69
|
+
memory: 13296 KB
|
|
70
|
+
truncated: false
|
|
71
|
+
output:
|
|
72
|
+
1267650600228229401496703205376
|
|
73
|
+
error: (empty)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
run_tests = OnlineCompilerRunTests()
|
|
78
|
+
print(
|
|
79
|
+
run_tests.invoke(
|
|
80
|
+
{
|
|
81
|
+
"compiler": "python-3.14",
|
|
82
|
+
"code": "a, b = map(int, input().split())\nprint(a - b)",
|
|
83
|
+
"tests": [
|
|
84
|
+
{"input": "2 3", "expected_output": "5"},
|
|
85
|
+
{"input": "10 4", "expected_output": "14"},
|
|
86
|
+
],
|
|
87
|
+
}
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
verdict: wrong_answer
|
|
94
|
+
score: 0
|
|
95
|
+
passed: 0 of 2
|
|
96
|
+
summary: 0 of 2 sample tests passed. Sample 1 expected 5 but the program printed -1.
|
|
97
|
+
test 1: wrong_answer (0.0338 s, exit_code 0)
|
|
98
|
+
output:
|
|
99
|
+
-1
|
|
100
|
+
test 2: wrong_answer (0.0378 s, exit_code 0)
|
|
101
|
+
output:
|
|
102
|
+
6
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Use them in an agent
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
# pip install -U langchain langchain-openai
|
|
109
|
+
from langchain.agents import create_agent
|
|
110
|
+
from langchain_onlinecompiler import OnlineCompilerToolkit
|
|
111
|
+
|
|
112
|
+
agent = create_agent(
|
|
113
|
+
model="openai:gpt-5-mini",
|
|
114
|
+
tools=OnlineCompilerToolkit().get_tools(),
|
|
115
|
+
system_prompt="Run code to check your answers instead of guessing.",
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
result = agent.invoke(
|
|
119
|
+
{
|
|
120
|
+
"messages": [
|
|
121
|
+
{
|
|
122
|
+
"role": "user",
|
|
123
|
+
"content": "What is the 90th Fibonacci number? Compute it.",
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
}
|
|
127
|
+
)
|
|
128
|
+
print(result["messages"][-1].content)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Any chat model with tool calling works. The tools also work with LangGraph's `ToolNode` and anything else that takes LangChain tools.
|
|
132
|
+
|
|
133
|
+
## The tools
|
|
134
|
+
|
|
135
|
+
### `onlinecompiler_run_code`
|
|
136
|
+
|
|
137
|
+
Calls `POST /api/run-code-sync/`.
|
|
138
|
+
|
|
139
|
+
| Argument | Type | Description |
|
|
140
|
+
| --- | --- | --- |
|
|
141
|
+
| `compiler` | string | A compiler id such as `python-3.14`, `nodejs-24`, `gcc-15`, `g++-15`, `openjdk-25`, `go-1.26`, `rust-1.93` or `sqlite-3`. All ids: [onlinecompiler.io/docs/compilers](https://onlinecompiler.io/docs/compilers). |
|
|
142
|
+
| `code` | string | The whole program, as one source file. |
|
|
143
|
+
| `input` | string, optional | Standard input for the program. |
|
|
144
|
+
| `time_limit` | number, optional | Seconds the program may run, above 0 and at most 30 (the default). |
|
|
145
|
+
|
|
146
|
+
The model reads the status, exit code (with its meaning for 124, 137 and the other codes the sandbox sets), time, memory, whether output was cut, the output and the error text. Files the program writes to `$OUTPUT_DIR` are listed by name and link.
|
|
147
|
+
|
|
148
|
+
### `onlinecompiler_run_tests`
|
|
149
|
+
|
|
150
|
+
Calls `POST /api/run-tests/`.
|
|
151
|
+
|
|
152
|
+
| Argument | Type | Description |
|
|
153
|
+
| --- | --- | --- |
|
|
154
|
+
| `compiler` | string | A compiler id, as above. |
|
|
155
|
+
| `code` | string | The whole program, as one source file. |
|
|
156
|
+
| `tests` | list | Test cases, each `{"input": "...", "expected_output": "..."}`; `input` is optional. |
|
|
157
|
+
| `checker` | string, optional | How output is compared: `trim` (default), `exact`, `tokens`, `float`, `rows`, `table` or `table_ordered`. |
|
|
158
|
+
|
|
159
|
+
The model reads the overall verdict, the score, the passed count, a plain-words summary, any compile error and a line per test. A visible test that failed also shows what the program printed; a hidden test shows only its verdict. Each test runs for up to 2 seconds in its own sandbox.
|
|
160
|
+
|
|
161
|
+
### Full results as the artifact
|
|
162
|
+
|
|
163
|
+
Both tools use `response_format="content_and_artifact"`. Called with a tool call (as an agent does), a tool returns a `ToolMessage` whose `content` is the short text above and whose `artifact` is the API's full JSON answer: for a run, every field including output file contents; for run-tests, every test result, the submission id and the signed receipt.
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
message = run_code.invoke(
|
|
167
|
+
{
|
|
168
|
+
"args": {"compiler": "python-3.14", "code": "print('hi')"},
|
|
169
|
+
"id": "call_1",
|
|
170
|
+
"name": "onlinecompiler_run_code",
|
|
171
|
+
"type": "tool_call",
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
message.artifact["exit_code"] # 0
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Async
|
|
178
|
+
|
|
179
|
+
Both tools support `ainvoke`. The `onlinecompiler` client is synchronous (standard library only), so an async call runs it in a worker thread and does not block the event loop.
|
|
180
|
+
|
|
181
|
+
## The sandbox
|
|
182
|
+
|
|
183
|
+
- 46 languages, from Python, JavaScript, TypeScript, C, C++, Java, Go and Rust to SQL, R and COBOL.
|
|
184
|
+
- Every run gets a fresh, isolated sandbox with **no network access**: code cannot call APIs or install packages. Popular packages are preinstalled (for Python: NumPy, pandas, SciPy, scikit-learn, Matplotlib and more).
|
|
185
|
+
- Up to 30 seconds and 512 MB per run; output is capped at 64 KB per stream.
|
|
186
|
+
- Each run counts as one run against your key's allowance; for run-tests, each test counts as one run.
|
|
187
|
+
|
|
188
|
+
Current limits: [onlinecompiler.io/docs](https://onlinecompiler.io/docs).
|
|
189
|
+
|
|
190
|
+
## Errors
|
|
191
|
+
|
|
192
|
+
| HTTP status | What the tool does |
|
|
193
|
+
| --- | --- |
|
|
194
|
+
| 400 (bad request, such as an unknown compiler id) | Returns an error `ToolMessage` to the model with the API's message, so it can correct the call. |
|
|
195
|
+
| 413 (request body over 32 MB) | Returns an error `ToolMessage` to the model. |
|
|
196
|
+
| 429 (a limit: runs at once, runs a minute, pending runs or the monthly allowance) | Returns an error `ToolMessage` that says which limit; nothing ran and nothing was counted. |
|
|
197
|
+
| 5xx, or no answer | Returns an error `ToolMessage`. |
|
|
198
|
+
| 401, 403 or 404 (the API key was refused) | Raises `onlinecompiler.AuthenticationError` (also exported from this package), since the model cannot fix a key. |
|
|
199
|
+
|
|
200
|
+
Errors the model can act on are `OnlineCompilerToolError` (429 is the subclass `OnlineCompilerRateLimitError`), a `ToolException`. The tools set `handle_tool_error=True` so these come back to the model; pass `handle_tool_error=False` to have them raised instead. Neither tool retries on its own, because every accepted run counts.
|
|
201
|
+
|
|
202
|
+
## Settings
|
|
203
|
+
|
|
204
|
+
| Field | Default | Description |
|
|
205
|
+
| --- | --- | --- |
|
|
206
|
+
| `api_key` | `ONLINECOMPILER_API_KEY` | Your API key (type API). |
|
|
207
|
+
| `base_url` | `https://api.onlinecompiler.io` | Where the API is. |
|
|
208
|
+
| `timeout` | `90` | Seconds to wait for an answer. |
|
|
209
|
+
|
|
210
|
+
## Development
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
pip install -e . pytest pytest-asyncio pytest-socket "langchain-tests>=1.1.9,<1.2"
|
|
214
|
+
pytest tests/unit_tests --allow-hosts=127.0.0.1 # offline, against a fake API
|
|
215
|
+
ONLINECOMPILER_API_KEY=... pytest tests/integration_tests # live; uses about 20 runs
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT. Copyright (c) 2026 RunCode Inc.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# langchain-onlinecompiler
|
|
2
|
+
|
|
3
|
+
LangChain tools for [OnlineCompiler.io](https://onlinecompiler.io): let an agent run code, or judge code against test cases, in 46 languages. Each run gets a fresh sandbox with no network access.
|
|
4
|
+
|
|
5
|
+
- `OnlineCompilerRunCode` (tool name `onlinecompiler_run_code`) runs a program and returns its output, errors, exit code, time and memory.
|
|
6
|
+
- `OnlineCompilerRunTests` (tool name `onlinecompiler_run_tests`) judges a program against test cases and returns an overall verdict, a score from 0 to 100 and a verdict per test.
|
|
7
|
+
- `OnlineCompilerToolkit` gives you both.
|
|
8
|
+
|
|
9
|
+
The tools call the OnlineCompiler.io REST API through the official [`onlinecompiler`](https://pypi.org/project/onlinecompiler/) Python client.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install -U langchain-onlinecompiler
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Get an API key
|
|
18
|
+
|
|
19
|
+
Create a free key at [api.onlinecompiler.io/compilers/api-keys/](https://api.onlinecompiler.io/compilers/api-keys/) (key type **API**). The free plan includes 100,000 runs a month. Then set it in the environment:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
export ONLINECOMPILER_API_KEY="your-api-key"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or pass it to a tool or the toolkit as `api_key="..."`. The key is held as a pydantic `SecretStr`, so it does not show up in reprs or logs.
|
|
26
|
+
|
|
27
|
+
## Use the tools directly
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from langchain_onlinecompiler import OnlineCompilerRunCode, OnlineCompilerRunTests
|
|
31
|
+
|
|
32
|
+
run_code = OnlineCompilerRunCode()
|
|
33
|
+
print(run_code.invoke({"compiler": "python-3.14", "code": "print(2 ** 100)"}))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
status: success
|
|
38
|
+
exit_code: 0
|
|
39
|
+
time: 0.0309 s
|
|
40
|
+
memory: 13296 KB
|
|
41
|
+
truncated: false
|
|
42
|
+
output:
|
|
43
|
+
1267650600228229401496703205376
|
|
44
|
+
error: (empty)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
run_tests = OnlineCompilerRunTests()
|
|
49
|
+
print(
|
|
50
|
+
run_tests.invoke(
|
|
51
|
+
{
|
|
52
|
+
"compiler": "python-3.14",
|
|
53
|
+
"code": "a, b = map(int, input().split())\nprint(a - b)",
|
|
54
|
+
"tests": [
|
|
55
|
+
{"input": "2 3", "expected_output": "5"},
|
|
56
|
+
{"input": "10 4", "expected_output": "14"},
|
|
57
|
+
],
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
verdict: wrong_answer
|
|
65
|
+
score: 0
|
|
66
|
+
passed: 0 of 2
|
|
67
|
+
summary: 0 of 2 sample tests passed. Sample 1 expected 5 but the program printed -1.
|
|
68
|
+
test 1: wrong_answer (0.0338 s, exit_code 0)
|
|
69
|
+
output:
|
|
70
|
+
-1
|
|
71
|
+
test 2: wrong_answer (0.0378 s, exit_code 0)
|
|
72
|
+
output:
|
|
73
|
+
6
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Use them in an agent
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
# pip install -U langchain langchain-openai
|
|
80
|
+
from langchain.agents import create_agent
|
|
81
|
+
from langchain_onlinecompiler import OnlineCompilerToolkit
|
|
82
|
+
|
|
83
|
+
agent = create_agent(
|
|
84
|
+
model="openai:gpt-5-mini",
|
|
85
|
+
tools=OnlineCompilerToolkit().get_tools(),
|
|
86
|
+
system_prompt="Run code to check your answers instead of guessing.",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
result = agent.invoke(
|
|
90
|
+
{
|
|
91
|
+
"messages": [
|
|
92
|
+
{
|
|
93
|
+
"role": "user",
|
|
94
|
+
"content": "What is the 90th Fibonacci number? Compute it.",
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
print(result["messages"][-1].content)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Any chat model with tool calling works. The tools also work with LangGraph's `ToolNode` and anything else that takes LangChain tools.
|
|
103
|
+
|
|
104
|
+
## The tools
|
|
105
|
+
|
|
106
|
+
### `onlinecompiler_run_code`
|
|
107
|
+
|
|
108
|
+
Calls `POST /api/run-code-sync/`.
|
|
109
|
+
|
|
110
|
+
| Argument | Type | Description |
|
|
111
|
+
| --- | --- | --- |
|
|
112
|
+
| `compiler` | string | A compiler id such as `python-3.14`, `nodejs-24`, `gcc-15`, `g++-15`, `openjdk-25`, `go-1.26`, `rust-1.93` or `sqlite-3`. All ids: [onlinecompiler.io/docs/compilers](https://onlinecompiler.io/docs/compilers). |
|
|
113
|
+
| `code` | string | The whole program, as one source file. |
|
|
114
|
+
| `input` | string, optional | Standard input for the program. |
|
|
115
|
+
| `time_limit` | number, optional | Seconds the program may run, above 0 and at most 30 (the default). |
|
|
116
|
+
|
|
117
|
+
The model reads the status, exit code (with its meaning for 124, 137 and the other codes the sandbox sets), time, memory, whether output was cut, the output and the error text. Files the program writes to `$OUTPUT_DIR` are listed by name and link.
|
|
118
|
+
|
|
119
|
+
### `onlinecompiler_run_tests`
|
|
120
|
+
|
|
121
|
+
Calls `POST /api/run-tests/`.
|
|
122
|
+
|
|
123
|
+
| Argument | Type | Description |
|
|
124
|
+
| --- | --- | --- |
|
|
125
|
+
| `compiler` | string | A compiler id, as above. |
|
|
126
|
+
| `code` | string | The whole program, as one source file. |
|
|
127
|
+
| `tests` | list | Test cases, each `{"input": "...", "expected_output": "..."}`; `input` is optional. |
|
|
128
|
+
| `checker` | string, optional | How output is compared: `trim` (default), `exact`, `tokens`, `float`, `rows`, `table` or `table_ordered`. |
|
|
129
|
+
|
|
130
|
+
The model reads the overall verdict, the score, the passed count, a plain-words summary, any compile error and a line per test. A visible test that failed also shows what the program printed; a hidden test shows only its verdict. Each test runs for up to 2 seconds in its own sandbox.
|
|
131
|
+
|
|
132
|
+
### Full results as the artifact
|
|
133
|
+
|
|
134
|
+
Both tools use `response_format="content_and_artifact"`. Called with a tool call (as an agent does), a tool returns a `ToolMessage` whose `content` is the short text above and whose `artifact` is the API's full JSON answer: for a run, every field including output file contents; for run-tests, every test result, the submission id and the signed receipt.
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
message = run_code.invoke(
|
|
138
|
+
{
|
|
139
|
+
"args": {"compiler": "python-3.14", "code": "print('hi')"},
|
|
140
|
+
"id": "call_1",
|
|
141
|
+
"name": "onlinecompiler_run_code",
|
|
142
|
+
"type": "tool_call",
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
message.artifact["exit_code"] # 0
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Async
|
|
149
|
+
|
|
150
|
+
Both tools support `ainvoke`. The `onlinecompiler` client is synchronous (standard library only), so an async call runs it in a worker thread and does not block the event loop.
|
|
151
|
+
|
|
152
|
+
## The sandbox
|
|
153
|
+
|
|
154
|
+
- 46 languages, from Python, JavaScript, TypeScript, C, C++, Java, Go and Rust to SQL, R and COBOL.
|
|
155
|
+
- Every run gets a fresh, isolated sandbox with **no network access**: code cannot call APIs or install packages. Popular packages are preinstalled (for Python: NumPy, pandas, SciPy, scikit-learn, Matplotlib and more).
|
|
156
|
+
- Up to 30 seconds and 512 MB per run; output is capped at 64 KB per stream.
|
|
157
|
+
- Each run counts as one run against your key's allowance; for run-tests, each test counts as one run.
|
|
158
|
+
|
|
159
|
+
Current limits: [onlinecompiler.io/docs](https://onlinecompiler.io/docs).
|
|
160
|
+
|
|
161
|
+
## Errors
|
|
162
|
+
|
|
163
|
+
| HTTP status | What the tool does |
|
|
164
|
+
| --- | --- |
|
|
165
|
+
| 400 (bad request, such as an unknown compiler id) | Returns an error `ToolMessage` to the model with the API's message, so it can correct the call. |
|
|
166
|
+
| 413 (request body over 32 MB) | Returns an error `ToolMessage` to the model. |
|
|
167
|
+
| 429 (a limit: runs at once, runs a minute, pending runs or the monthly allowance) | Returns an error `ToolMessage` that says which limit; nothing ran and nothing was counted. |
|
|
168
|
+
| 5xx, or no answer | Returns an error `ToolMessage`. |
|
|
169
|
+
| 401, 403 or 404 (the API key was refused) | Raises `onlinecompiler.AuthenticationError` (also exported from this package), since the model cannot fix a key. |
|
|
170
|
+
|
|
171
|
+
Errors the model can act on are `OnlineCompilerToolError` (429 is the subclass `OnlineCompilerRateLimitError`), a `ToolException`. The tools set `handle_tool_error=True` so these come back to the model; pass `handle_tool_error=False` to have them raised instead. Neither tool retries on its own, because every accepted run counts.
|
|
172
|
+
|
|
173
|
+
## Settings
|
|
174
|
+
|
|
175
|
+
| Field | Default | Description |
|
|
176
|
+
| --- | --- | --- |
|
|
177
|
+
| `api_key` | `ONLINECOMPILER_API_KEY` | Your API key (type API). |
|
|
178
|
+
| `base_url` | `https://api.onlinecompiler.io` | Where the API is. |
|
|
179
|
+
| `timeout` | `90` | Seconds to wait for an answer. |
|
|
180
|
+
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
pip install -e . pytest pytest-asyncio pytest-socket "langchain-tests>=1.1.9,<1.2"
|
|
185
|
+
pytest tests/unit_tests --allow-hosts=127.0.0.1 # offline, against a fake API
|
|
186
|
+
ONLINECOMPILER_API_KEY=... pytest tests/integration_tests # live; uses about 20 runs
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
MIT. Copyright (c) 2026 RunCode Inc.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "OnlineCompiler.io integration"
|
|
3
|
+
description: "Integrate with the OnlineCompiler.io run-code and run-tests tools using LangChain Python."
|
|
4
|
+
integration:
|
|
5
|
+
name: OnlineCompiler.io
|
|
6
|
+
pypi: langchain-onlinecompiler
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
[OnlineCompiler.io](https://onlinecompiler.io) is a code execution API: it runs a program in 46 languages and returns its output, or judges it against test cases and returns a verdict per test. Each run gets a fresh gVisor sandbox with no network access. This guide covers the two OnlineCompiler.io [tools](/oss/langchain/tools): `OnlineCompilerRunCode` and `OnlineCompilerRunTests`. For every setting and error, see the [package README](https://github.com/onlinecompiler-io/langchain-onlinecompiler/tree/main/python#readme).
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
### Details
|
|
14
|
+
|
|
15
|
+
| Class | Package | Serializable | JS support | Version |
|
|
16
|
+
| :--- | :--- | :---: | :---: | :---: |
|
|
17
|
+
| `OnlineCompilerRunCode` | [`langchain-onlinecompiler`](https://pypi.org/project/langchain-onlinecompiler/) | ❌ | ❌ |  |
|
|
18
|
+
| `OnlineCompilerRunTests` | [`langchain-onlinecompiler`](https://pypi.org/project/langchain-onlinecompiler/) | ❌ | ❌ |  |
|
|
19
|
+
|
|
20
|
+
### Tool features
|
|
21
|
+
|
|
22
|
+
| [Returns artifact](/oss/langchain/tools) | Native async | Return data | Pricing |
|
|
23
|
+
| :---: | :---: | :---: | :---: |
|
|
24
|
+
| ✅ | ❌ (`ainvoke` runs in a worker thread) | output, error, exit code, time, memory; or verdict, score and per-test verdicts | 100,000 free runs / month |
|
|
25
|
+
|
|
26
|
+
- 46 languages, including Python, JavaScript, TypeScript, C, C++, Java, C#, Go, Rust, PHP, Ruby, SQL and R.
|
|
27
|
+
- A fresh sandbox for every run, with no network access, up to 30 seconds and 512 MB.
|
|
28
|
+
- Judging with seven output checkers (`trim`, `exact`, `tokens`, `float`, `rows`, `table`, `table_ordered`); hidden tests return only their verdict.
|
|
29
|
+
- The model reads a short text result; the full JSON answer (with output files and, for judged runs, a signed receipt) is the `ToolMessage` artifact.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Setup
|
|
34
|
+
|
|
35
|
+
To use the tools, create a free OnlineCompiler.io API key and install the `langchain-onlinecompiler` package.
|
|
36
|
+
|
|
37
|
+
### Credentials
|
|
38
|
+
|
|
39
|
+
Create a key of type API at [api.onlinecompiler.io/compilers/api-keys/](https://api.onlinecompiler.io/compilers/api-keys/). The free plan includes 100,000 runs a month.
|
|
40
|
+
|
|
41
|
+
```python Set API key icon="key"
|
|
42
|
+
import getpass
|
|
43
|
+
import os
|
|
44
|
+
|
|
45
|
+
if "ONLINECOMPILER_API_KEY" not in os.environ:
|
|
46
|
+
os.environ["ONLINECOMPILER_API_KEY"] = getpass.getpass("Enter your OnlineCompiler.io API key: ")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It's also helpful (but not needed) to set up LangSmith for best-in-class observability/<Tooltip tip="Log each step of a model's execution to debug and improve it">tracing</Tooltip> of your tool calls. To enable automated tracing, set your [LangSmith](/langsmith/observability) API key:
|
|
50
|
+
|
|
51
|
+
```python Enable tracing icon="flask"
|
|
52
|
+
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
|
|
53
|
+
os.environ["LANGSMITH_TRACING"] = "true"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Installation
|
|
57
|
+
|
|
58
|
+
The OnlineCompiler.io tools live in the `langchain-onlinecompiler` package:
|
|
59
|
+
|
|
60
|
+
<CodeGroup>
|
|
61
|
+
```python pip
|
|
62
|
+
pip install -U langchain-onlinecompiler
|
|
63
|
+
```
|
|
64
|
+
```python uv
|
|
65
|
+
uv add langchain-onlinecompiler
|
|
66
|
+
```
|
|
67
|
+
</CodeGroup>
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Instantiation
|
|
72
|
+
|
|
73
|
+
Both tools read `ONLINECOMPILER_API_KEY`; you can also pass `api_key`. The toolkit gives you both tools with one set of settings.
|
|
74
|
+
|
|
75
|
+
```python Initialize tool instance icon="robot"
|
|
76
|
+
from langchain_onlinecompiler import (
|
|
77
|
+
OnlineCompilerRunCode,
|
|
78
|
+
OnlineCompilerRunTests,
|
|
79
|
+
OnlineCompilerToolkit,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
run_code = OnlineCompilerRunCode()
|
|
83
|
+
run_tests = OnlineCompilerRunTests()
|
|
84
|
+
|
|
85
|
+
tools = OnlineCompilerToolkit().get_tools() # both
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Invocation
|
|
91
|
+
|
|
92
|
+
### Directly
|
|
93
|
+
|
|
94
|
+
`onlinecompiler_run_code` takes `compiler` (a compiler id such as `python-3.14`, `nodejs-24`, `gcc-15` or `openjdk-25`; [all ids](https://onlinecompiler.io/docs/compilers)), `code`, and optionally `input` (standard input) and `time_limit` (seconds, at most 30).
|
|
95
|
+
|
|
96
|
+
```python Call tool icon="rocket"
|
|
97
|
+
print(run_code.invoke({"compiler": "python-3.14", "code": "print(2 ** 100)"}))
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
status: success
|
|
102
|
+
exit_code: 0
|
|
103
|
+
time: 0.0309 s
|
|
104
|
+
memory: 13296 KB
|
|
105
|
+
truncated: false
|
|
106
|
+
output:
|
|
107
|
+
1267650600228229401496703205376
|
|
108
|
+
error: (empty)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`onlinecompiler_run_tests` takes `compiler`, `code`, `tests` (each `{"input": ..., "expected_output": ...}`) and optionally `checker`. Each test counts as one run.
|
|
112
|
+
|
|
113
|
+
```python Judge code icon="rocket"
|
|
114
|
+
print(
|
|
115
|
+
run_tests.invoke(
|
|
116
|
+
{
|
|
117
|
+
"compiler": "python-3.14",
|
|
118
|
+
"code": "a, b = map(int, input().split())\nprint(a - b)",
|
|
119
|
+
"tests": [
|
|
120
|
+
{"input": "2 3", "expected_output": "5"},
|
|
121
|
+
{"input": "10 4", "expected_output": "14"},
|
|
122
|
+
],
|
|
123
|
+
}
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
```text
|
|
129
|
+
verdict: wrong_answer
|
|
130
|
+
score: 0
|
|
131
|
+
passed: 0 of 2
|
|
132
|
+
summary: 0 of 2 sample tests passed. Sample 1 expected 5 but the program printed -1.
|
|
133
|
+
test 1: wrong_answer (0.0338 s, exit_code 0)
|
|
134
|
+
output:
|
|
135
|
+
-1
|
|
136
|
+
test 2: wrong_answer (0.0378 s, exit_code 0)
|
|
137
|
+
output:
|
|
138
|
+
6
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### As a ToolCall
|
|
142
|
+
|
|
143
|
+
We can also invoke the tool with a model-generated `ToolCall`, in which case a @[`ToolMessage`] will be returned. Its `artifact` is the API's full JSON answer:
|
|
144
|
+
|
|
145
|
+
```python ToolCall icon="briefcase"
|
|
146
|
+
# This is usually generated by a model, but we'll create a tool call directly for demo purposes.
|
|
147
|
+
model_generated_tool_call = {
|
|
148
|
+
"args": {"compiler": "python-3.14", "code": "print(input()[::-1])", "input": "stressed"},
|
|
149
|
+
"id": "1",
|
|
150
|
+
"name": run_code.name,
|
|
151
|
+
"type": "tool_call",
|
|
152
|
+
}
|
|
153
|
+
message = run_code.invoke(model_generated_tool_call)
|
|
154
|
+
message.artifact["output"] # 'desserts\n'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Within an agent
|
|
158
|
+
|
|
159
|
+
We can use the tools in an agent. For this we will need a model with tool-calling capabilities.
|
|
160
|
+
|
|
161
|
+
```python Agent with tool icon="robot"
|
|
162
|
+
# pip install -qU langchain langchain-openai
|
|
163
|
+
from langchain.agents import create_agent
|
|
164
|
+
|
|
165
|
+
agent = create_agent(
|
|
166
|
+
model="openai:gpt-5-mini",
|
|
167
|
+
tools=OnlineCompilerToolkit().get_tools(),
|
|
168
|
+
system_prompt="Run code to check your answers instead of guessing.",
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# Run the agent
|
|
172
|
+
agent.invoke(
|
|
173
|
+
{"messages": [{"role": "user", "content": "What is the 90th Fibonacci number? Compute it."}]}
|
|
174
|
+
)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Errors
|
|
180
|
+
|
|
181
|
+
A refusal the model can act on comes back to it as an error `ToolMessage`: an unknown compiler id or another bad request (HTTP 400), a request over 32 MB (413), a limit such as runs at once or the monthly allowance (429, where nothing ran and nothing was counted), or a failure on the service's side. Pass `handle_tool_error=False` to have these raised as `OnlineCompilerToolError` instead. A refused API key (HTTP 401, 403 or 404) is always raised as `AuthenticationError`, since the model cannot fix it.
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## API reference
|
|
186
|
+
|
|
187
|
+
For all OnlineCompiler.io tool settings, see the [package README](https://github.com/onlinecompiler-io/langchain-onlinecompiler/tree/main/python#readme). For the REST API itself, see [onlinecompiler.io/docs](https://onlinecompiler.io/docs).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""LangChain tools for OnlineCompiler.io: run and judge code in 46 languages in a sandbox."""
|
|
2
|
+
|
|
3
|
+
from onlinecompiler import AuthenticationError
|
|
4
|
+
|
|
5
|
+
from langchain_onlinecompiler.toolkits import OnlineCompilerToolkit
|
|
6
|
+
from langchain_onlinecompiler.tools import (
|
|
7
|
+
OnlineCompilerRateLimitError,
|
|
8
|
+
OnlineCompilerRunCode,
|
|
9
|
+
OnlineCompilerRunTests,
|
|
10
|
+
OnlineCompilerToolError,
|
|
11
|
+
RunCodeInput,
|
|
12
|
+
RunTestsInput,
|
|
13
|
+
TestCase,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__version__ = "0.1.0"
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"AuthenticationError",
|
|
20
|
+
"OnlineCompilerRateLimitError",
|
|
21
|
+
"OnlineCompilerRunCode",
|
|
22
|
+
"OnlineCompilerRunTests",
|
|
23
|
+
"OnlineCompilerToolError",
|
|
24
|
+
"OnlineCompilerToolkit",
|
|
25
|
+
"RunCodeInput",
|
|
26
|
+
"RunTestsInput",
|
|
27
|
+
"TestCase",
|
|
28
|
+
"__version__",
|
|
29
|
+
]
|
|
File without changes
|