Skip to content
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

Move over unstructured parser #31390

Merged
merged 66 commits into from
Oct 26, 2023
Merged

Conversation

flash1293
Copy link
Contributor

@flash1293 flash1293 commented Oct 13, 2023

What

Closes #31614

This PR moves the unstructured parser currently in the S3 connector over to the CDK.
This will enable unstructured file extraction for all connectors based on the file based CDK.

⚠️ Based on the dependencies, the hi_res parsing strategy can't be applied - it will default to fast and fall back to ocr_only if necessary. ⚠️

Open questions:

  • Should there be one parser per file type? The downside of that is that the user needs to configure more stuff
  • Should it be possible to opt out of unstructured files? Maybe there are some file sources that don't want to support this

@vercel
Copy link

vercel bot commented Oct 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
airbyte-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 26, 2023 10:38am

@octavia-squidington-iii octavia-squidington-iii added area/connectors Connector related issues area/documentation Improvements or additions to documentation CDK Connector Development Kit labels Oct 13, 2023
@github-actions
Copy link
Contributor

Before Merging a Connector Pull Request

Wow! What a great pull request you have here! 🎉

To merge this PR, ensure the following has been done/considered for each connector added or updated:

  • PR name follows PR naming conventions
  • Breaking changes are considered. If a Breaking Change is being introduced, ensure an Airbyte engineer has created a Breaking Change Plan.
  • Connector version has been incremented in the Dockerfile and metadata.yaml according to our Semantic Versioning for Connectors guidelines
  • You've updated the connector's metadata.yaml file any other relevant changes, including a breakingChanges entry for major version bumps. See metadata.yaml docs
  • Secrets in the connector's spec are annotated with airbyte_secret
  • All documentation files are up to date. (README.md, bootstrap.md, docs.md, etc...)
  • Changelog updated in docs/integrations/<source or destination>/<name>.md with an entry for the new version. See changelog example
  • Migration guide updated in docs/integrations/<source or destination>/<name>-migrations.md with an entry for the new version, if the version is a breaking change. See migration guide example
  • If set, you've ensured the icon is present in the platform-internal repo. (Docs)

If the checklist is complete, but the CI check is failing,

  1. Check for hidden checklists in your PR description

  2. Toggle the github label checklist-action-run on/off to re-run the checklist CI.

@flash1293 flash1293 requested a review from aaronsteers October 23, 2023 11:19
Joe Reuter and others added 4 commits October 23, 2023 13:24
Comment on lines 64 to 67
def _read_file(self, file_handle: IOBase, file_name: str) -> str:
from unstructured.file_utils.filetype import FileType
from unstructured.partition.auto import partition
from unstructured.partition.md import optional_decode
Copy link
Collaborator

@aaronsteers aaronsteers Oct 25, 2023

Choose a reason for hiding this comment

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

Regarding these inline imports, are these not on the top of the file because they are extras in the CDK, and because they might not be available at runtime if the extra is not included?

I think the general best practice for these scenarios would be something like this at the top of the file...

try:
    import unstructured
    from unstructured.file_utils.filetype import FileType
    from unstructured.partition.auto import partition
    from unstructured.partition.md import optional_decode
except ImportError as ex:
    logger.warning(f"Unstructured libraries could not be imported. {ex}")
    FileType = None
    partition = None
    optional_decode = None

Then within the individual functions, we can either assume that the code path will not be traversed without the dependencies, or in certain points we can just check the needed import at top of the function.

    def _read_file(self, file_handle: IOBase, file_name: str) -> str:
        if unstructured is none:
            raise ImportError("Unstructured library is not available. Please install any missing libraries (...) and try again.")

Copy link
Contributor Author

@flash1293 flash1293 Oct 25, 2023

Choose a reason for hiding this comment

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

They are inline because they are quite slow to import - if they are on the top of the file it would slow down the connector even if the parser is not used because python has to load so much code. I can add a comment to explain this (or maybe there is a more elegant way to handle this I’m not aware of)

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case, what do you think about putting into a helper function called by the class constructor?

Pseudocode, may need adjustment:

...
unstructured_lib = None

def _import_unstructured():
    """Dynamically imported as needed, due to slow import speed."""
    global unstructured_lib

    import unstructured
    unstructured_lib = unstructured

class UnstructuredParser(FileTypeParser):
    def __init__():
        _import_unstructured()

@flash1293
Copy link
Contributor Author

Thanks for the review @aaronsteers , I changed the way the dynamic imports are handled, could you take another look?

Copy link
Contributor

@clnoll clnoll left a comment

Choose a reason for hiding this comment

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

LGTM @flash1293!

@flash1293 flash1293 requested a review from aaronsteers October 25, 2023 15:40
Copy link
Collaborator

@aaronsteers aaronsteers left a comment

Choose a reason for hiding this comment

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

Looks good!
Thanks for those changes. 👍

@flash1293 flash1293 merged commit e3793c1 into master Oct 26, 2023
16 checks passed
@flash1293 flash1293 deleted the flash1293/move-over-unstructured-parser branch October 26, 2023 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDK Connector Development Kit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move document file type parser to CDK
4 participants