Skip to content

[Platform] Enhance in memory platform to support all ResultInterface types #252

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

Conversation

RamyHakam
Copy link
Contributor

@RamyHakam RamyHakam commented Aug 3, 2025

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues none
License MIT

Enhances the existing InMemoryPlatform to support all ResultInterface types, making it easier to test all result types.
Previously limited to text responses, it now supports vectors, images, binary data, choices, and any custom result types.

@RamyHakam RamyHakam force-pushed the Enhance-InMemoryPlatform-to-support-all-ResultInterface-types branch from a8c3a8f to a5e6fa0 Compare August 3, 2025 15:53
Comment on lines 36 to 46
public function invoke(Model $model, array|string|object $input, array $options = []): ResultPromise
{
$resultText = $this->mockResult instanceof \Closure
? ($this->mockResult)($model, $input, $options)
: $this->mockResult;
if ($this->mockResult instanceof \Closure) {
$result = ($this->mockResult)($model, $input, $options);

if ($result instanceof ResultInterface) {
return new ResultPromise(
static fn () => $result,
rawResult: $result->getRawResult() ?? new InMemoryRawResult(
['text' => $result->getContent()],
),
options: $options
);
}

$resultText = (string) $result;
} else {
$resultText = $this->mockResult;
}

$textResult = new TextResult($resultText);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, yes, but would like to see the code less complex please.
For example this could also work, right?

    public function invoke(Model $model, array|string|object $input, array $options = []): ResultPromise
    {
        $result = is_string($this->mockResult) ? $this->mockResult : ($this->mockResult)($model, $input, $options);

        if ($result instanceof ResultInterface) {
            return $this->createPromise($result, $options);
        }

        return $this->createPromise(new TextResult($result), $options);
    }

    private function createPromise(ResultInterface $result, array $options): ResultPromise
    {
        $rawResult = $result->getRawResult() ?? new InMemoryRawResult(
            ['text' => $result->getContent()],
            (object) ['text' => $result->getContent()],
        );

        return new ResultPromise(static fn () => $result, $rawResult, $options);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, that is simpler and less complex

@RamyHakam RamyHakam requested a review from chr-hertel August 4, 2025 10:17
@fabpot fabpot changed the title [Platform] Enhance in memory platform to support all ResultInterface types Enhance in memory platform to support all ResultInterface types Aug 4, 2025
@chr-hertel chr-hertel added the Platform Issues & PRs about the AI Platform component label Aug 4, 2025
@fabpot fabpot changed the title Enhance in memory platform to support all ResultInterface types [Platform] Enhance in memory platform to support all ResultInterface types Aug 4, 2025
@chr-hertel chr-hertel force-pushed the Enhance-InMemoryPlatform-to-support-all-ResultInterface-types branch from d00b887 to 82e873f Compare August 4, 2025 18:52
@chr-hertel
Copy link
Member

Thank you @RamyHakam.

@chr-hertel chr-hertel merged commit 9265cbc into symfony:main Aug 4, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform Issues & PRs about the AI Platform component Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants