Skip to content

Fix dereferencing in Function and AsyncFunction #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion replicate/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,17 @@ def _resolve_ref(obj: Any) -> Any:

result = _resolve_ref(dereferenced)

# Filter out any references that have now been referenced.
# Remove "paths" as these aren't relevant to models.
result["paths"] = {}

# Retain Input and Output schemas as these are important.
dereferenced_refs.discard("Input")
dereferenced_refs.discard("Output")

dereferenced_refs.discard("TrainingInput")
dereferenced_refs.discard("TrainingOutput")

# Filter out any remaining references that have been inlined.
result["components"]["schemas"] = {
k: v
for k, v in result["components"]["schemas"].items()
Expand Down
90 changes: 90 additions & 0 deletions tests/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,82 @@ def create_mock_version(version_overrides=None, version_id="xyz123"):
"required": ["prompt"],
},
"Output": {"type": "string", "title": "Output"},
"PredictionResponse": {
"type": "object",
"title": "PredictionResponse",
"properties": {
"id": {"type": "string", "title": "Id"},
"logs": {"type": "string", "title": "Logs", "default": ""},
"error": {"type": "string", "title": "Error"},
"input": {"$ref": "#/components/schemas/Input"},
"output": {"$ref": "#/components/schemas/Output"},
"status": {"$ref": "#/components/schemas/Status"},
"metrics": {"type": "object", "title": "Metrics"},
"version": {"type": "string", "title": "Version"},
"created_at": {
"type": "string",
"title": "Created At",
"format": "date-time",
},
"started_at": {
"type": "string",
"title": "Started At",
"format": "date-time",
},
"completed_at": {
"type": "string",
"title": "Completed At",
"format": "date-time",
},
},
},
"PredictionRequest": {
"type": "object",
"title": "PredictionRequest",
"properties": {
"id": {"type": "string", "title": "Id"},
"input": {"$ref": "#/components/schemas/Input"},
"webhook": {
"type": "string",
"title": "Webhook",
"format": "uri",
"maxLength": 65536,
"minLength": 1,
},
"created_at": {
"type": "string",
"title": "Created At",
"format": "date-time",
},
"output_file_prefix": {
"type": "string",
"title": "Output File Prefix",
},
"webhook_events_filter": {
"type": "array",
"items": {"$ref": "#/components/schemas/WebhookEvent"},
"default": ["start", "output", "logs", "completed"],
},
},
},
"Status": {
"enum": [
"starting",
"processing",
"succeeded",
"canceled",
"failed",
],
"type": "string",
"title": "Status",
"description": "An enumeration.",
},
"WebhookEvent": {
"enum": ["start", "output", "logs", "completed"],
"type": "string",
"title": "WebhookEvent",
"description": "An enumeration.",
},
}
},
},
Expand Down Expand Up @@ -345,6 +421,7 @@ async def test_use_function_openapi_schema_dereferenced(client_mode):
"openapi_schema": {
"components": {
"schemas": {
"Extra": {"type": "object"},
"Output": {"$ref": "#/components/schemas/ModelOutput"},
"ModelOutput": {
"type": "object",
Expand Down Expand Up @@ -374,6 +451,12 @@ async def test_use_function_openapi_schema_dereferenced(client_mode):
else:
schema = hotdog_detector.openapi_schema()

assert schema["components"]["schemas"]["Extra"] == {"type": "object"}
assert schema["components"]["schemas"]["Input"] == {
"type": "object",
"properties": {"prompt": {"type": "string", "title": "Prompt"}},
"required": ["prompt"],
}
assert schema["components"]["schemas"]["Output"] == {
"type": "object",
"properties": {
Expand All @@ -386,7 +469,14 @@ async def test_use_function_openapi_schema_dereferenced(client_mode):
},
}

# Assert everything else is stripped out
assert schema["paths"] == {}

assert "PredictionRequest" not in schema["components"]["schemas"]
assert "PredictionResponse" not in schema["components"]["schemas"]
assert "ModelOutput" not in schema["components"]["schemas"]
assert "Status" not in schema["components"]["schemas"]
assert "WebhookEvent" not in schema["components"]["schemas"]


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.