Skip to content

Commit

Permalink
Fixup int tests (#397)
Browse files Browse the repository at this point in the history
still flakey though
  • Loading branch information
hinthornw authored Feb 2, 2024
1 parent ff87144 commit 9f34ad1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 49 deletions.
15 changes: 8 additions & 7 deletions .github/actions/js-integration-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ inputs:
node-version:
description: "Node version"
required: true
langchain-api-key:
description: "Langchain"
required: true
openai-api-key:
description: "OpenAI API key"
required: false
runs:
using: "composite"
steps:
Expand All @@ -28,10 +34,5 @@ runs:
working-directory: js
env:
LANGCHAIN_TRACING_V2: "true"
LANGCHAIN_API_KEY: ${{ secrets.LANGCHAIN_API_KEY }}


- name: Run JS integration tests
run: yarn test:integration
shell: bash
working-directory: js
LANGCHAIN_API_KEY: ${{ inputs.langchain-api-key }}
OPENAI_API_KEY: ${{ inputs.openai-api-key }}
13 changes: 10 additions & 3 deletions .github/actions/python-integration-tests/action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
name: "Integration Tests"
name: "Python Integration Tests"
description: "Run integration tests"
inputs:
python-version:
description: "Python version"
required: true
langchain-api-key:
description: "Langchain"
required: true
openai-api-key:
description: "OpenAI API key"
required: false
runs:
using: "composite"
steps:
Expand All @@ -17,7 +23,7 @@ runs:
cache: "pip"

- name: Install poetry
run: pipx install poetry==$POETRY_VERSION
run: pipx install poetry==1.4.2
shell: bash
working-directory: python

Expand All @@ -28,8 +34,9 @@ runs:

- name: Run integration tests
env:
LANGCHAIN_API_KEY: ${{ secrets.LANGCHAIN_API_KEY }}
LANGCHAIN_TRACING_V2: "true"
LANGCHAIN_API_KEY: ${{ inputs.langchain-api-key }}
OPENAI_API_KEY: ${{ inputs.openai-api-key }}
run: make integration_tests
shell: bash
working-directory: python
8 changes: 6 additions & 2 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
uses: ./.github/actions/python-integration-tests
with:
python-version: 3.11
langchain-api-key: ${{ secrets.LANGCHAIN_API_KEY }}
openai-api-key: ${{ secrets.OPENAI_API_KEY }}

js_integration_test:
name: JS Integration Test
Expand All @@ -40,12 +42,14 @@ jobs:
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: "yarn"
cache-dependency-path: "js/yarn.lock"
- name: Install dependencies
run: yarn install --immutable
- name: Run JS integration tests
uses: ./.github/actions/js-integration-tests
with:
node-version: 18.x
node-version: 20.x
langchain-api-key: ${{ secrets.LANGCHAIN_API_KEY }}
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
34 changes: 15 additions & 19 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ async function waitUntilRunFound(
try {
const run = await client.readRun(runId);
if (checkOutputs) {
return run.outputs !== null && run.outputs !== undefined;
return (
run.outputs !== null &&
run.outputs !== undefined &&
Object.keys(run.outputs).length !== 0
);
}
return true;
} catch (e) {
Expand Down Expand Up @@ -116,12 +120,11 @@ test.concurrent("Test LangSmith Client Dataset CRD", async () => {
expect(examples.length).toBe(2);
expect(examples.map((e) => e.id)).toContain(example.id);

const newExampleResponse = await client.updateExample(example.id, {
await client.updateExample(example.id, {
inputs: { col1: "updatedExampleCol1" },
outputs: { col2: "updatedExampleCol2" },
});
// Says 'example updated' or something similar
console.log(newExampleResponse);
const newExampleValue = await client.readExample(example.id);
expect(newExampleValue.inputs.col1).toBe("updatedExampleCol1");
await client.deleteExample(example.id);
Expand All @@ -142,8 +145,8 @@ test.concurrent(
async () => {
const langchainClient = new Client({});

const projectName = "__test_evaluate_run";
const datasetName = "__test_evaluate_run_dataset";
const projectName = "__test_evaluate_run" + Date.now();
const datasetName = "__test_evaluate_run_dataset" + Date.now();
await deleteProject(langchainClient, projectName);
await deleteDataset(langchainClient, datasetName);

Expand Down Expand Up @@ -260,8 +263,12 @@ test.concurrent(
);
expect(fetchedFeedback[0].value).toEqual("INCORRECT");

await langchainClient.deleteDataset({ datasetId: dataset.id });
await langchainClient.deleteProject({ projectName });
try {
await langchainClient.deleteDataset({ datasetId: dataset.id });
await langchainClient.deleteProject({ projectName });
} catch (e) {
console.log(e);
}
},
160_000
);
Expand All @@ -282,11 +289,8 @@ test.concurrent("Test persist update run", async () => {

await langchainClient.updateRun(runId, { outputs: { output: ["Hi"] } });
await waitUntilRunFound(langchainClient, runId, true);

const storedRun = await langchainClient.readRun(runId);
expect(storedRun.id).toEqual(runId);
expect(storedRun.outputs).toEqual({ output: ["Hi"] });

await langchainClient.deleteProject({ projectName });
});

Expand Down Expand Up @@ -552,15 +556,7 @@ describe("createChatExample", () => {
const langchainClient = new Client({});

const datasetName = "__createChatExample-test-dataset";
try {
const existingDataset = await langchainClient.readDataset({
datasetName,
});
await langchainClient.deleteDataset({ datasetId: existingDataset.id });
} catch (e) {
console.log("Dataset does not exist");
}

await deleteDataset(langchainClient, datasetName);
const dataset = await langchainClient.createDataset(datasetName);

const input = [new HumanMessage({ content: "Hello, world!" })];
Expand Down
3 changes: 3 additions & 0 deletions python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ tests_watch:
poetry run ptw --now . -- -vv -x tests/unit_tests

integration_tests:
poetry run pytest tests/integration_tests

integration_tests_fast:
poetry run pytest -n auto tests/integration_tests

lint:
Expand Down
47 changes: 29 additions & 18 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The LangSmith Client."""

from __future__ import annotations

import collections
Expand Down Expand Up @@ -151,24 +152,30 @@ def _default_retry_config() -> Retry:
return Retry(**retry_params) # type: ignore


_PRIMITIVE_TYPES = (str, int, float, bool, tuple, list, dict)
_MAX_DEPTH = 3
_PRIMITIVE_TYPES = (str, int, float, bool)
_MAX_DEPTH = 2


def _serialize_json(obj: Any, depth: int = 0) -> Any:
try:
if depth >= _MAX_DEPTH:
try:
return json.loads(json.dumps(obj))
except BaseException:
return repr(obj)
if isinstance(obj, datetime.datetime):
return obj.isoformat()
if isinstance(obj, uuid.UUID):
return str(obj)
if obj is None or isinstance(obj, _PRIMITIVE_TYPES):
return obj
if isinstance(obj, set):
return list(obj)
if isinstance(obj, bytes):
return obj.decode("utf-8")
if depth >= _MAX_DEPTH:
return repr(obj)
if isinstance(obj, (set, list, tuple)):
return [_serialize_json(x, depth + 1) for x in list(obj)]
if isinstance(obj, dict):
return {k: _serialize_json(v, depth + 1) for k, v in obj.items()}

serialization_methods = [
("model_dump_json", True), # Pydantic V2
("json", True), # Pydantic V1
Expand Down Expand Up @@ -1213,9 +1220,9 @@ def list_runs(
body_query: Dict[str, Any] = {
"session": [project_id] if project_id else None,
"run_type": run_type,
"reference_example": [reference_example_id]
if reference_example_id
else None,
"reference_example": (
[reference_example_id] if reference_example_id else None
),
"query": query,
"filter": filter,
"execution_order": execution_order,
Expand Down Expand Up @@ -1626,9 +1633,11 @@ def get_test_results(
row[f"feedback.{k}"] = v.get("avg")
row.update(
{
"execution_time": (r.end_time - r.start_time).total_seconds()
if r.end_time
else None,
"execution_time": (
(r.end_time - r.start_time).total_seconds()
if r.end_time
else None
),
"error": r.error,
"id": r.id,
}
Expand Down Expand Up @@ -2056,9 +2065,9 @@ def create_chat_example(
final_generations = cast(dict, generations)
return self.create_example(
inputs={"input": final_input},
outputs={"output": final_generations}
if final_generations is not None
else None,
outputs=(
{"output": final_generations} if final_generations is not None else None
),
dataset_id=dataset_id,
dataset_name=dataset_name,
created_at=created_at,
Expand Down Expand Up @@ -2806,9 +2815,11 @@ def list_annotation_queues(
name_contains: Optional[str] = None,
) -> Iterator[ls_schemas.AnnotationQueue]:
params: dict = {
"ids": [_as_uuid(id_, f"queue_ids[{i}]") for i, id_ in enumerate(queue_ids)]
if queue_ids is not None
else None,
"ids": (
[_as_uuid(id_, f"queue_ids[{i}]") for i, id_ in enumerate(queue_ids)]
if queue_ids is not None
else None
),
"name": name,
"name_contains": name_contains,
}
Expand Down

0 comments on commit 9f34ad1

Please sign in to comment.