Skip to content

[1.x] Let similarity search subclasses override the search - #1237

Open
medilies wants to merge 3 commits into
laravel:1.xfrom
medilies:feat/similarity-search-request-to-closure
Open

medilies wants to merge 3 commits into
laravel:1.xfrom
medilies:feat/similarity-search-request-to-closure

Conversation

@medilies

@medilies medilies commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

SimilaritySearch only gives its closure the query string. A subclass can add arguments to schema() (tags, a category, a result count), but nothing can read them, so the only way to use them today is to copy handle() into the subclass. The copy then drifts from the package: the result normalization, the reranking, the empty-results message and the output format all have to be kept in sync by hand.

This PR moves the closure call into a protected search() method that subclasses can override. The closure still receives only the query string, so existing callables are unaffected:

class DocsSearch extends SimilaritySearch
{
    public function schema(JsonSchema $schema): array
    {
        return [
            ...parent::schema($schema),
            'tags' => $schema->array()->items($schema->string())->required(),
        ];
    }

    protected function search(Request $request): mixed
    {
        return Doc::query()
            ->whereVectorSimilarTo('embedding', $request->string('query'))
            ->whereJsonContains('tags', $request->array('tags'))
            ->get();
    }
}

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@medilies
medilies marked this pull request as ready for review November 15, 2026 14:21
@pushpak1300

Copy link
Copy Markdown
Member

Thanks for this! The gap is real, but changing what the closure receives is a breaking change on 1.x: callables with an optional second parameter, PHP built-in functions passed as callables, and variadic closures would all start receiving the Request.

Since adding fields to schema() already means writing a subclass, could you move the hook there instead? Leave the closure call as it was, and extract it into a protected method that subclasses can override:

public function handle(Request $request): string
{
    $results = $this->search($request);

    // ...
}

protected function search(Request $request): mixed
{
    return call_user_func($this->using, $request->string('query'));
}

A subclass then reads its own arguments and still gets the normalization, reranking and output format from the parent:

class DocsSearch extends SimilaritySearch
{
    public function schema(JsonSchema $schema): array
    {
        return [
            ...parent::schema($schema),
            'tags' => $schema->array()->items($schema->string())->required(),
        ];
    }

    protected function search(Request $request): mixed
    {
        return Doc::query()
            ->whereVectorSimilarTo('embedding', $request->string('query'))
            ->whereJsonContains('tags', $request->array('tags'))
            ->get();
    }
}

For the test, a subclass that overrides search() and returns the request's tags is enough, with an assertion that they appear in the handle() output. Then the upgrade note can come out of the description. Thanks!

@pushpak1300
pushpak1300 marked this pull request as draft November 20, 2026 19:16
@medilies medilies changed the title [1.x] Pass the tool request to the similarity search closure [1.x] Let similarity search subclasses override the search Nov 21, 2026
@medilies
medilies marked this pull request as ready for review November 21, 2026 12:27
@medilies

Copy link
Copy Markdown
Contributor Author

Thanks, done. The closure call is back to passing only the query, and it now lives in a protected search(Request $request) method that subclasses can override. The test uses a subclass that returns the request's tags, and the upgrade note is gone from the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants